LinkedIn API 获取 /v2/me 返回"Unpermitted fields present in PARAMETER"



问题

我正在尝试运行requests-oauth2 linkedIn示例。我已经能够解决一些问题,示例已过时,但似乎无法正确运行。
运行程序的响应对象内容:

b'{" serviceErrorCode":100," message":"参数中存在的未经操纵字段:数据处理异常在处理字段[/access_token]","状态":403}

系统和版本

  • Python 3.6.8
  • 请求2.22.0
  • 请求-Oauthlib 1.2.0
    通过终端运行所有内容。

尝试

  1. 首先,应用程序设置具有正确的权限r_liteprofile
  2. 我确认我正在使用正确的范围进行认证。
  3. 我尝试在Get请求中添加各种标题。
  4. i主机在请求对象中打印了参数变量的内容,并发现它是一个空的dict。

代码

我已经添加了评论来解释我在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)

有什么想法吗?建议?方向?

更新02/17/2020

根据对GitHub存储库的文档和评论,https://github.com/requests/requests-oauthlib linkedIn合规性修复程序已过时并导致错误。此后,维护者删除了LinkedIn合规性修复代码,并将多个更新应用于LinkedIn示例,作为PR#397的一部分。这不再是问题。

下面的原始答案

最终我通过的请求URL包含一个未经操作的字段。URL的手动审查显示了两个字段:

  1. oauth2_access_token
  2. access_token

查看OAuth2-RQuests源代码,第二个字段在提出最终请求之前就添加到URL中。

requests-oauthlib/requests_oauthlib/oauth2_session.py

我认为有一种防止这种行为的机制,但我找不到它,我对他们的github和其他地方的评论/问题没有得到解答。我的解决方案是在我的项目中使用request()方法中的此肮脏的修复程序复制oauth2_session.py模块的修改版。

old_version_url = url
url, headers, data = self._client.add_token(url, http_method=method, body=data, headers=headers)
# Dirty work around to prevent the `access_token` parameter from being added
# to the url, causing a unpermitted parameters error requesting linkedin resource.
if "&access_token=" in url:
    url = old_version_url

可以在此GitHub repo,Linkedin_assist/linkedin_assist/quick_fixes/oauth2_session.py

中找到整个修改的模块。

最新更新