简体   繁体   English

领英 REST API 认证

[英]LinkedIn REST API Authentication

I have created an app with the LinkedIn API .我用LinkedIn API创建了一个应用程序。 For some strange reason none of the authentication attempts work.由于某些奇怪的原因,所有身份验证尝试均无效。 I followed this tutorial based on the Python-LinkedIn interface .我遵循了基于Python-LinkedIn 接口本教程 I get the authentication URL to paste into a web browser but no log-in page is displayed.我获得了身份验证 URL 以粘贴到 web 浏览器中,但没有显示登录页面。 The error message I get from LinkedIn is not helpful, ie "something went wrong" (see picture).我从 LinkedIn 收到的错误消息没有帮助,即“出了点问题”(见图)。

My code so far:到目前为止我的代码:

import requests 
import string
import random


clientID = "_cID from API__"
clientSecret = "__CS from API___" #
redirect_uri = "https://wemf.ch/de/"
letters = string.ascii_lowercase
csrfToken = ''.join(random.choice(letters) for i in range(24))

html = requests.get("https://www.linkedin.com/oauth/v2/authorization",
                   params = auth_params)
print(html.url)    

I have also tried to set the redirect URL to http://localhost:8000 as suggested here , here and here我还尝试按照此处此处此处的建议将重定向 URL 设置为 http://localhost:8000

I do not use any VPN.我不使用任何 VPN。 I have tried Edge, Firefox and Chrome.我试过 Edge、Firefox 和 Chrome。 They all show the same error message in the picture.它们都在图片中显示相同的错误消息。

Thanks for your help.谢谢你的帮助。

在此处输入图像描述

In the mean time I have found the answer.与此同时,我找到了答案。 The issue was a white space in the scopes.问题是作用域中有空白。
Before you run below code, check what access scopes your app has and adjust below code accordingly.在运行下面的代码之前,请检查您的应用程序具有哪些访问范围并相应地调整下面的代码。 The code implements the 3-legged oauth flow for access to the Marketing Development Platform (MDP) .该代码实现了用于访问营销开发平台 (MDP)三足 oauth 流程

Step 1: Get the authorisation code from the URL第一步:从URL获取授权码

import requests

client_id = '_your_client_ID_'
client_secret = '_your_client_secret'

base_url = "https://www.linkedin.com/oauth/v2/authorization"
redirect_uri = "https://www.linkedin.com/developers/tools/oauth/redirect"
#adjust scope. 
scope = "w_member_social,r_organization_social,w_organization_social,r_liteprofile"

url = f"{base_url}?response_type=code&client_id={client_id}&state=random&redirect_uri={redirect_uri}&scope={scope}"
print(url)

auth_code = "_copy_authorisation_code_from_the_URL"

--> the authorisation code is hidden in the URL displayed after you log-in to your LinkedIn account and give the app access permission. --> 授权码隐藏在您登录LinkedIn账户并授予应用程序访问权限后显示的URL中。 Here an example what it will look like:这是一个示例:
https://www.linkedin.com/developers/tools/oauth/redirect?code=___you_will_find_the_code_here___&state=random https://www.linkedin.com/developers/tools/oauth/redirect?code=___you_will_find_the_code_here___&state=random

Step 2: Get access token第 2 步:获取访问令牌

url_access_token = 'https://www.linkedin.com/oauth/v2/accessToken'

payload = {
    'grant_type' : 'authorization_code',
    'code' : auth_code,
    'redirect_uri' : redirect_uri,
    'client_id' : client_id,
    'client_secret' : client_secret
}

response = requests.post(url=url_access_token, params=payload)
response_json = response.json()
# Extract the access_token from the response_json
access_token = response_json['access_token']

Step 3: Get own profile data第 3 步:获取自己的个人资料数据
spoiler alert - it is not much data but shows that the authorisation worked.透警报——数据不多,但表明授权有效。 If you want your own LinkedIn data it is easier to request it online and you'll get all of your data in 2 zip files.如果您想要自己的 LinkedIn 数据,在线请求它会更容易,您将在 2 个 zip 文件中获得所有数据。

def get_profile(access_token):
    URL = "https://api.linkedin.com/v2/me"
    headers = {'Content-Type': 'application/x-www-form-urlencoded',
               'Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
    response = requests.get(url=URL, headers=headers)
    print(response.json())

get_profile(access_token)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM