我尝试使用 linkedin OAuth2 为我的 Django 应用程序集成社交登录。我已按照其官方页面此处中描述的步骤进行操作。在步骤2中,我为范围参数指定了“配置文件”值,下面给出的是我的应用程序中允许的范围。
。我已经成功完成了步骤3,并成功获得了access_token。但是,当我尝试使用该访问令牌进行身份验证的请求时,例如,curl -X GET https://api.linkedin.com/v2/userinfo,我收到类似 b'{"serviceErrorCode":100," 的错误message":"没有足够的权限来访问:GET /userinfo","status":403}'。我尝试过 v2/me,得到相同的权限错误。
下面给出的是代码片段。
def login(request):
return render(request, 'login.html')
def authenticate(request):
redirect_url = f'https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={settings.SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY}&redirect_uri={settings.LOGIN_REDIRECT_URL}&state={settings.STATE}&scope=profile'
return redirect(redirect_url)
def complete(request):
code = request.GET.get('code', None)
state = request.GET.get('state', None)
url = "https://www.linkedin.com/oauth/v2/accessToken"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
payload = {
'grant_type': 'authorization_code',
'code': code,
'client_id': settings.SOCIAL_AUTH_LINKEDIN_OAUTH2_KEY,
'client_secret': settings.SOCIAL_AUTH_LINKEDIN_OAUTH2_SECRET,
'redirect_uri': settings.LOGIN_REDIRECT_URL
}
response = requests.post(url, headers=headers, data=payload)
if response.status_code == 200:
access_token = json.loads(response.content)['access_token']
print(access_token)
info_url = 'https://api.linkedin.com/v2/userinfo'
headers = {'Authorization': f'Bearer {access_token}'}
info_response = requests.get(info_url, headers=headers)
print(info_response.content)
# getting error here like b'{"serviceErrorCode":100,"message":"Not enough permissions to access: GET /userinfo","status":403}'
如何获得范围“个人资料”的许可?根据他们的文档here“个人资料”具有开放权限。我不明白我的代码中缺少什么。重定向网址是否有任何限制,例如是否应该是 https?
这有什么好运气吗?看到同样的事情