Python-缺少“ client_id”。怎么解决?

时间:2019-03-10 15:30:23

标签: python django linkedin django-allauth linkedin-api

我一直在使用LinkedIn api(OAuth 2),并找到了一个示例来对其进行测试。我已按照本教程进行操作,但是由于某种原因(如代码中的要求)提供了完整的重定向URL时,出现错误:(invalid_request) A required parameter "client_id" is missing。我不确定自己在做什么错,但是如果有人有任何想法,我将感谢您的反馈。

在寻找解决方案时,我发现另一个人为此苦苦挣扎:"client_id" is missing when authenticate with LinkedIn

这是示例中的代码:

Linkedin.py

from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix

# Credentials you get from registering a new application
client_id = SECRET
client_secret = SECRET

# OAuth endpoints given in the LinkedIn API documentation
authorization_base_url = 'https://www.linkedin.com/uas/oauth2/authorization'
token_url = 'https://www.linkedin.com/uas/oauth2/accessToken'

linkedin = OAuth2Session(client_id, redirect_uri='http://localhost:8000')
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
linkedin.fetch_token(token_url, client_secret=client_secret,authorization_response=redirect_response)

# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print (r.content)

链接到示例:https://requests-oauthlib.readthedocs.io/en/latest/examples/linkedin.html

其他说明:我使用的教程没有日期。我只能假设API教程中使用的链接正确且最新。

3 个答案:

答案 0 :(得分:2)

问题出在URLs上,我写了一个类似的程序,对我来说效果很好:

from requests_oauthlib import OAuth2Session
from requests_oauthlib.compliance_fixes import linkedin_compliance_fix


# Credentials you get from registering a new application
client_id = '<the client id you get from linkedin>'
client_secret = '<the client secret you get from linkedin>'
redirect_url = '<authorized redirect URL from LinkedIn config>'

# OAuth endpoints given in the LinkedIn API documentation (you can check for the latest updates)
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)
linkedin = OAuth2Session(client_id, redirect_uri=redirect_url)
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
linkedin.fetch_token(token_url, client_secret=client_secret,
                     authorization_response=redirect_response)

# Fetch a protected resource, i.e. user profile
r = linkedin.get('https://api.linkedin.com/v1/people/~')
print(r.content)

希望对您有帮助!

答案 1 :(得分:1)

这个版本有些旧,但是我想我需要从oauth请求文档站点上对LinkedIn示例进行一些其他更改。

与更新的链接一起,似乎在使用验证者代码交换令牌时,LinkedIn期望请求正文中的client_id。我不确定要在哪里或何时将其遗忘,但是在深入研究oauth-request源代码后,我发现fetch方法具有一个强制将client_id包含在请求中的参数正文(include_client_id)添加到fetch方法中应该可以使示例工作。

linkedin.fetch_token(token_url, client_secret=client_secret,
                     authorization_response=redirect_response,
                     include_client_id=True)

答案 2 :(得分:0)

虽然这可能不是造成问题的原因,但您使用的是LinkedIn身份验证URL的旧版本。根据LinkedIn的OAuth文档(https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/consumer/context),您的authorziation_base_url应该为

https://www.linkedin.com/oauth/v2/authorization