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

时间:2020-07-19 20:12:36

标签: python api python-requests http-headers

我正在尝试连接到FatSecret API。这是他们为oauth2授权提供的文档的链接:https://platform.fatsecret.com/api/Default.aspx?screen=rapiauth2#using-token-api

我已经完成了步骤1和2,现在停留在步骤3。我设法用'application / json'更新了标头,但是我不确定我的调用中是否包含令牌I创造。我得到一个响应码200,但随后出现一条错误消息: {'错误':{'代码':21,'消息':“检测到无效的IP地址:'51 .7.45.86'”}}

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

1 个答案:

答案 0 :(得分:0)

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)

应该这样做