使用Pinterest API v5获取Access Token失败:OAuth流程受阻
解决Pinterest API v5 OAuth获取Access Token时的"Missing request body"错误
问题根源
你把grant_type、code这类参数通过params传给了requests.post,这会让参数拼在URL的查询字符串里,但Pinterest的v5 OAuth token接口要求这些参数必须放在请求体中(对应你设置的application/x-www-form-urlencoded格式),所以接口返回了"Missing request body"错误。
修正后的代码
将params替换为data参数,让参数进入请求体:
import requests import base64 client_id= 'my_client_id' client_secret = 'my_client_secret' data_string = f'{client_id}:{client_secret}' token = base64.b64encode(data_string.encode()) headers = { 'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'application/x-www-form-urlencoded', } url = "https://api.pinterest.com/v5/oauth/token" code = "my_code_that_i_got_in_the_first_step" # 用data替代params,将参数放入请求体 data = { 'grant_type':'authorization_code', 'code': code, 'redirect_uri':'https://my_redirect_uri' } r = requests.post(url, headers=headers, data=data) print(r.json())
关键注意事项
- 参数名是
redirect_uri而非你代码里的redirect_url,这是Pinterest API规定的正确字段名,写错会导致认证失败。 - 确保
redirect_uri和你在Pinterest开发者后台配置的地址完全一致,包括协议、域名、路径,不能有任何差异。
内容的提问来源于stack exchange,提问作者notDarkMatter
相关产品推荐
相关产品推荐

