简体   繁体   English

Python 使用令牌 (oauth2) FatSecret API 请求授权 header

[英]Python requests Authorization header with token (oauth2) FatSecret API

I'm trying to connect to the FatSecret API. Here's the link to the documentation they give for an oauth2 authorization: https://platform.fatsecret.com/api/Default.aspx?screen=rapiauth2#using-token-api我正在尝试连接到 FatSecret API。这是他们为 oauth2 授权提供的文档的链接: https://platform.fatsecret.com/api/Default.aspx?screen=rapiauth2#using-token-api

I've made it through steps 1 and 2 and now I'm stuck on step 3. I've managed to update the headers with 'application/json' but I'm not sure if my call includes the token I create.我已经完成了第 1 步和第 2 步,现在我停留在第 3 步。我已经设法使用“application/json”更新标头,但我不确定我的调用是否包含我创建的令牌。 I get a response code 200 but then an error message: {'error': {'code': 21, 'message': "Invalid IP address detected: '51.7.45.XX'"}}我收到响应代码 200,但随后收到一条错误消息:{'error': {'code': 21, 'message': "Invalid IP address detected: '51.7.45.XX'"}}

params = {
    'grant_type': 'client_credentials',
    'client_id': 'REMOVED',
    'client_secret': 'REMOVED',
    'scope': 'basic',
    'method' : 'foods.search',
    'search_expression' : 'toast',
    'format' : 'json',
}

api_url = 'https://platform.fatsecret.com/rest/server.api'


response = requests.post(api_url, headers={'content-type' : 'application/json', 'Authorization' : 'Bearer {}'.format(data['access_token'])}, params=params)

Error Code错误代码

params = {
    'grant_type': 'client_credentials',
    'client_id': 'REMOVED',
    'client_secret': 'REMOVED',
    'scope': 'basic',
    'method' : 'foods.search',
    'search_expression' : 'toast',
    'format' : 'json'
}

api_url = 'https://platform.fatsecret.com/rest/server.api'


response = requests.post(api_url, headers={'Authorization' : f'Bearer {data["access_token"]}'}, json=params)

Should do it应该这样做

You have to login into your fastsecret account and whitelist your local IP there.你必须登录到你的 fastsecret 帐户并将你本地的 IP 列入白名单。 Once you add your IP to get whitelisted it takes around 1 hour to get approved.一旦您将 IP 添加到白名单中,大约需要 1 小时才能获得批准。

This resolved the issue for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM