【问题标题】:LinkedIn API get /v2/me returns "Unpermitted fields present in PARAMETER"LinkedIn API get /v2/me 返回“参数中存在未经许可的字段”
【发布时间】: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
    通过终端运行所有内容。

尝试

  1. 首先应用程序设置有正确的权限r_liteprofile
  2. 我确认我正在使用正确的范围进行身份验证。
  3. 我尝试向 get 请求添加各种标头。
  4. 我在控制台打印了请求对象中 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


    【解决方案1】:

    2020 年 2 月 17 日更新

    根据 github 存储库上的文档和 cmets,https://github.com/requests/requests-oauthlib LinkedIn 合规性修复已过时并导致错误。作为PR #397 的一部分,维护人员已经删除了LinkedIn 合规性修复代码,并对LinkedIn 示例应用了一些更新。这应该不再是问题了。

    原答案如下

    最终,我传递的请求 url 包含一个未经许可的字段。人工审核 url 后发现两个字段:

    1. oauth2_access_token
    2. access_token

    查看 OAuth2-Requests 源代码,第二个字段在发出最终请求之前添加到 url。

    requests-oauthlib/requests_oauthlib/oauth2_session.py

    我想有一种机制可以防止这种行为,但我找不到它,我在他们的 github 和其他地方的 cmets/questions 没有得到答复。我的解决方案是在我的项目中复制 oauth2_session.py 模块的修改版本,并在 request() 方法中进行此脏修复。

    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

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2021-12-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多