如何使用Python调用LinkedIn API?
我尝试了很多方法,但似乎都不管用。帮助我使用python与LinkedIn建立连接。生成访问令牌时出现问题我收到了代码,但它不起作用。我有Python3.9,请发布一个建立连接并获取访问令牌的基本代码示例。以及我必须使用的重定向URI。我可以使用任何网站链接进行重新构建 我试图通过curl和Postman检查API,但没有得到解决方案,比如说未经授权的访问。如何使用Python调用LinkedIn API?,python,api,authorization,linkedin,linkedin-api,Python,Api,Authorization,Linkedin,Linkedin Api,我尝试了很多方法,但似乎都不管用。帮助我使用python与LinkedIn建立连接。生成访问令牌时出现问题我收到了代码,但它不起作用。我有Python3.9,请发布一个建立连接并获取访问令牌的基本代码示例。以及我必须使用的重定向URI。我可以使用任何网站链接进行重新构建 我试图通过curl和Postman检查API,但没有得到解决方案,比如说未经授权的访问。 第一个解决方案适用于任何(包括免费)应用程序,它使用所谓的3腿OAuth 2.0身份验证: 在浏览器中登录到您的帐户 通过创建新的应用程序
第一个解决方案适用于任何(包括免费)应用程序,它使用所谓的
3腿OAuth 2.0身份验证
:
import webbrowser;webbrowser.open(url)
code。在所有字段中也填写您的值。代码中有redirect\u uri
,这是返回授权响应的URL,对于本地运行的脚本,您必须运行Python HTTP web服务器才能检索结果redirect\u uri
,两个字段code
和state
将附加到此URL,code
是您应该存储在服务器上的唯一授权代码,code
在30分钟后过期,如果不使用,状态
是上面代码中状态的副本,此状态类似于当前授权会话的唯一id,仅使用同一个状态字符串一次,每次随机生成,并且状态不是秘密,因为您在授权URL内将其发送给用户,但应是唯一的且相当长的。完整重定向URL的示例为https://your.server.com/linkedin_authorized_callback?code=987ab12uiu98onvokm56&state=D5B1C1348F110D7C
code
以通过下一个代码访问\u令牌,下一个代码应该在您的服务器上或您的应用程序运行的地方运行,因为它使用您的应用程序的client\u secret
,并且这是一个秘密值,您不应该向公众显示它,永远不要与任何人共享ClientSecret
,可能除了一些值得信任的人,因为这些人有能力假装(伪造)是您的应用程序,而他们不是
访问\u令牌
有效期为60天
!这么长时间。如果你计划只为自己或朋友使用你的应用程序,那么你可以在两个月内手动为几个人预生成一次代币,而不需要服务器
access\u token
进行任何API调用。在所有调用中包括授权:承载访问\u令牌HTTP头。下面是一个此类API代码的示例:
- 您的应用程序完全在远程服务器上运行,这意味着身份验证和正在运行的应用程序(API调用)都是在某个专用的远程服务器上完成的。这样就不会有安全问题,服务器不会共享任何机密,例如
,客户端\u机密
,代码
访问\u令牌
- 您的应用程序在用户机器上本地运行,而服务器偶尔会运行一次身份验证,服务器还可以执行一些其他操作,例如在数据库中存储必要的数据。然后,您的服务器不需要共享
,客户机密钥
,而是共享代码
,该令牌将被发送回应用程序到用户的机器。也可以,这样您的服务器就可以跟踪哪些用户正在使用您的应用程序,如果需要阻止用户,还可以撤销部分或全部访问令牌
access\u令牌
- 您的应用程序完全在本地用户的机器上运行,根本不使用专用服务器。在这种情况下,
、client\u secret
、code
都存储在用户的机器上。在这种情况下,您无法撤消某些特定用户对应用程序的访问,您只能通过在应用程序设置中重新生成access\u token
来撤消所有这些用户。此外,您无法跟踪应用程序用户的任何工作(尽管可能在应用程序设置/信息页面中有一些使用统计信息)。在这种情况下,任何用户都可以查看您的应用程序代码并复制client\u secret
,除非您将Python编译成一些客户端密码
/.exe
/.dll
并在那里加密您的客户端密码。如果任何人获得了。因此
他可以假装(伪造)是你的应用程序,这意味着如果你的应用程序以某种方式与其他用户联系,那么他可以通过显示你的应用程序界面来尝试授权其他人,而下面有一些其他欺诈代码,基本上你的应用程序不再那么安全或可信。此外,本地代码可以很容易地修改,所以您不应该相信您的应用程序能够准确地执行您的代码。此外,为了像在前面的步骤client\u secret
5)-7)中所做的那样对用户进行授权,如果是本地应用程序,您必须启动Python HTTP服务器才能检索步骤
5)的重定向结果
下面是一个第二个解决方案仅当您的应用程序是
LinkedIn Developer Enterprise Products
付费订阅的一部分时才有效,并且您需要在应用程序设置中启用客户端凭据流,下一步
# Needs: python -m pip install requests
import requests, secrets
url = requests.Request(
'GET',
'https://www.linkedin.com/oauth/v2/authorization',
params = {
'response_type': 'code', # Always should equal to fixed string "code"
# ClientID of your created application
'client_id': 'REPLACE_WITH_YOUR_CLIENT_ID',
# The URI your users are sent back to after authorization.
# This value must match one of the OAuth 2.0 Authorized Redirect
# URLs defined in your application configuration.
# This is basically URL of your server that processes authorized requests like:
# https://your.server.com/linkedin_authorized_callback
'redirect_uri': 'REPLACE_WITH_REDIRECT_URL', # Replace this with your value
# state, any unique non-secret randomly generated string like DCEeFWf45A53sdfKef424
# that identifies current authorization request on server side.
# One way of generating such state is by using standard "secrets" module like below.
# Store generated state string on your server for further identifying this authorization session.
'state': secrets.token_hex(8).upper(),
# Requested permissions, below is just example, change them to what you need.
# List of possible permissions is here:
# https://docs.microsoft.com/en-us/linkedin/shared/references/migrations/default-scopes-migration#scope-to-consent-message-mapping
'scope': '%20'.join(['r_liteprofile', 'r_emailaddress', 'w_member_social']),
},
).prepare().url
# You may now send this url from server to user
# Or if code runs locally just open browser like below
import webbrowser
webbrowser.open(url)
# Needs: python -m pip install requests
import requests
access_token = requests.post(
'https://www.linkedin.com/oauth/v2/accessToken',
params = {
'grant_type': 'authorization_code',
# This is code obtained on previous step by Python script.
'code': 'REPLACE_WITH_CODE',
# This should be same as 'redirect_uri' field value of previous Python script.
'redirect_uri': 'REPLACE_WITH_REDIRECT_URL',
# Client ID of your created application
'client_id': 'REPLACE_WITH_YOUR_CLIENT_ID',
# Client Secret of your created application
'client_secret': 'REPLACE_WITH_YOUR_CLIENT_SECRET',
},
).json()['access_token']
print(access_token)
import requests
print(requests.get(
'https://api.linkedin.com/v2/jobs',
params = {
# Any API params go here
},
headers = {
'Authorization': 'Bearer ' + access_token,
# Any other needed HTTP headers go here
},
).json())
# Needs: python -m pip install requests
import requests
access_token = requests.post(
'https://www.linkedin.com/oauth/v2/accessToken',
params = {
'grant_type': 'client_credentials',
'client_id': 'REPLACE_WITH_YOUR_CLIENT_ID',
'client_secret': 'REPLACE_WITH_YOUR_CLIENT_SECRET',
},
).json()['access_token']
print(access_token)
import requests
print(requests.get(
'https://api.linkedin.com/v2/jobs',
params = {
# Any API params go here
},
headers = {
'Authorization': 'Bearer ' + access_token,
# Any other needed HTTP headers go here
},
).json())