使用标头中的 API 密钥执行 GET API 调用的正确方法? [英] Proper way to perform GET API Call with API key in header?

查看:23
本文介绍了使用标头中的 API 密钥执行 GET API 调用的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未真正尝试过尝试编写自己的调用 API 的代码.我有一些在发现 Python Requests 库后创建的 Python 代码.

但是,我无法解决此身份验证失败"错误消息.

我获得的 API 密钥来自 https://fortnitetracker.com/site-api

我使用的代码如下:

导入请求url = 'https://api.fortnitetracker.com/v1/profile/pc/ninja'headers = {"TRN-Api-Key": "MY_KEY"}r = requests.get(url, headers=headers)

在询问 r.status_code 后,我得到 403 Forbidden.当我要求 r.text 我得到 <块引用>

u'{"message":"无效的身份验证凭据"}\n'

在 API 页面上,他们所说的只是使用 GET 方法在请求标头中传递 API 密钥.

我什至尝试传递我用来在网站上注册的凭据

r = requests.get(url, headers=headers,auth=('User', 'Pass'))

仍然出现相同的无效身份验证凭据错误.

我做的对吗?我错过了什么?

提前感谢您的帮助.

解决方案

如果您正在调用 API,您需要发送帖子:

r = requests.post(url, headers=headers)

I've never really attempted to try and write my own code that calls an API. I have some Python code that I created after discovering Python Requests library.

However, I can't get past this "authentication failed" error message.

The API key I have acquired is from https://fortnitetracker.com/site-api

The code I am using is as follows:

import requests
url = 'https://api.fortnitetracker.com/v1/profile/pc/ninja'
headers = {"TRN-Api-Key": "MY_KEY"}
r = requests.get(url, headers=headers)

After asking for the r.status_code I get a 403 Forbidden. When I ask for r.text I get

u'{"message":"Invalid authentication credentials"}\n'

On the API page,all they say is to pass the API key in the request headers using a GET method.

I even tried passing my credentials I used to register on the site using

r = requests.get(url, headers=headers,auth=('User', 'Pass'))

Still got the same invalid authentcation credentials error.

Is what I am doing correct? What am I missing?

Thanks for your help in advance.

解决方案

If you are calling an API you need to send post :

r = requests.post(url, headers=headers)

这篇关于使用标头中的 API 密钥执行 GET API 调用的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆