【发布时间】:2019-12-11 11:37:03
【问题描述】:
问题
我正在尝试运行requests-OAuth2 LinkedIn 示例。我已经能够解决示例已过时的一些问题,但似乎无法让最后一行正确运行。
运行程序得到的响应对象内容:
b'{"serviceErrorCode":100,"message":"PARAMETER 中存在不允许的字段:处理字段时出现数据处理异常 [/access_token]","status":403}
系统和版本
- Python 3.6.8
- 请求 2.22.0
- 请求-oauthlib 1.2.0
通过终端运行所有内容。
尝试
- 首先应用程序设置有正确的权限
r_liteprofile。 - 我确认我正在使用正确的范围进行身份验证。
- 我尝试向 get 请求添加各种标头。
- 我在控制台打印了请求对象中 PARAMETERS 变量的内容,发现它是一个空字典。
代码
我添加了 cmets 来解释我对 requests-oauthlib 网站上的股票教程所做的更改。
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix
# Credentials you get from registering a new application
client_id = vault.CLIENT_ID
client_secret = vault.CLIENT_SECRET
# CHANGE: Scope is necessary to avoid permission errors
scope = ['r_liteprofile', 'r_emailaddress', 'w_member_social']
redirect_url = 'http://127.0.0.1'
# OAuth endpoints given in the LinkedIn API documentation (you can check for the latest updates)
# CHANGE: updated urls
authorization_base_url = 'https://www.linkedin.com/oauth/v2/authorization'
token_url = 'https://www.linkedin.com/oauth/v2/accessToken'
# Authorized Redirect URL (from LinkedIn configuration)
# CHANGE: added scope argument to OAuth2Session init method
linkedin = OAuth2Session(client_id, redirect_uri=redirect_url, scope=scope)
linkedin = linkedin_compliance_fix(linkedin)
# Redirect user to LinkedIn for authorization
authorization_url, state = linkedin.authorization_url(authorization_base_url)
print('Please go here and authorize,', authorization_url)
# Get the authorization verifier code from the callback url
redirect_response = input('Paste the full redirect URL here:')
# Fetch the access token
# CHANGED: LinkedIn required client_id to be in body, flipped include_client_id to True
linkedin.fetch_token(token_url,client_secret=client_secret,
include_client_id=True,authorization_response=redirect_response)
# CHANGED: Just an example of a header I tried passing to the get method below
headers = {'X-Restli-Protocol-Version': '2.0.0'}
r = linkedin.get('https://api.linkedin.com/v2/me')
print(r.content)
有什么想法吗?建议?方向?
【问题讨论】:
标签: python oauth oauth-2.0 python-requests linkedin-api