Ank*_*ita 5 python authorization linkedin-api
我尝试了很多方法,但似乎没有一个有效。帮助我使用 python 与 LinkedIn 建立连接。生成访问令牌时出现问题 我收到了代码,但它不起作用。我有 python 3.9 请发布建立连接并获取访问令牌的基本代码示例。我必须使用哪个redirectUri。我可以使用 rediectUri 的任何网站链接吗?
我尝试通过curl和Postman检查API,但没有得到解决方案,它说“未经授权的访问”。 https://github.com/ozgur/python-linkedin <---这是我了解如何使用 API 的地方。接收访问令牌。
Art*_*oul 14
第一个适用于任何(包括免费)应用程序的解决方案,它使用所谓的3-Legged OAuth 2.0 Authentication:
import webbrowser; webbrowser.open(url)代码打开此 URL。也用您的值填写所有字段。redirect_uri代码中有一个,这是发送回授权响应的 URL,对于本地运行的脚本,您必须运行 Python HTTP Web 服务器来检索结果。# 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://learn.microsoft.com/en-us/linkedin/shared/references/migrations/default-scopes-migration#scope-to-consent-message-mapping
'scope': ' '.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)
Run Code Online (Sandbox Code Playgroud)
用户通过先前的 URL 授权您的应用程序后,他的浏览器将被重定向到redirect_uri两个字段code并将state附加到此 URL,code是您应存储在服务器上的唯一授权代码,如果不使用则code过期,是先前代码的状态副本上面,这个状态就像您当前授权会话的唯一ID,仅使用相同的状态字符串一次并每次随机生成它,而且状态也不是秘密,因为您将其发送给授权URL内的用户,但应该是唯一且相当的长的。完整重定向 URL 的示例是。30 minutesstatehttps://your.server.com/linkedin_authorized_callback?code=987ab12uiu98onvokm56&state=D5B1C1348F110D7C
接下来,您必须将code之前获得的值交换为access_token下一个代码,下一个代码应该在您的服务器或您的应用程序运行的地方运行,因为它使用client_secret您的应用程序,并且这是一个秘密值,您不应该将其公开,永远不要与任何人共享,ClientSecret除了一些值得信赖的人,因为这些人有能力假装(假冒)您的应用程序,而实际上他们不是。
# 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)
Run Code Online (Sandbox Code Playgroud)
access_token前面的脚本获得的有效期为60 days!所以时间相当长。如果您打算仅为自己或您的朋友使用您的应用程序,那么您只需每两个月手动为几个人预生成一次代币,而无需服务器。
接下来用于access_token代表刚刚授权的上述 LinkedIn 用户进行任何 API 调用。Authorization: Bearer ACCESS_TOKEN在所有调用中包含HTTP 标头。下面是此类 API 代码的示例:
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())
Run Code Online (Sandbox Code Playgroud)
client_secret,例如code,,,access_token。client_secret, code,而是共享access_token发送回应用程序到用户计算机的内容。access_token也可以,然后您的服务器可以跟踪哪些用户正在使用您的应用程序,如果需要阻止用户,也可以撤销部分或全部s。client_secret, code,access_token都存储在用户的计算机上。在这种情况下,您无法撤销某些特定用户对应用程序的访问权限,只能通过client_secret在应用程序设置中重新生成来撤销所有用户。此外,您无法跟踪应用程序用户的任何工作(尽管您的应用程序设置/信息页面中可能有一些使用统计信息)。在这种情况下,任何用户都可以查看您的应用程序代码并进行复制,client_secret除非您将 Python 编译为.exe//并在那里加密您的客户端密钥。如果有人得到了,他可以假装(假冒)你的应用程序,这意味着如果你的应用程序以某种方式联系其他用户,那么他可以尝试通过显示你的应用程序界面来授权其他人,同时在下面有一些其他欺诈代码,基本上你的应用程序并不那么安全或不再信任。此外,本地代码可以轻松修改,因此您不应该相信您的应用程序会完全执行您的代码。此外,为了像在本地应用程序的情况下在前面的步骤中完成的那样授权用户,您必须启动 Python HTTP Server 才能检索步骤的重定向结果。.dll.soclient_secret5)-7)5)以下是第二个解决方案,仅当您的应用程序是付费订阅的一部分时才有效LinkedIn Developer Enterprise Products,并且您还需要Enable Client Credentials Flow在应用程序设置中,后续步骤使用所谓的2-Legged OAuth 2.0 Authentication:
# 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)
Run Code Online (Sandbox Code Playgroud)
access_token之前的响应,它会在发出后 30 分钟后过期,因此您需要经常使用之前的脚本来获取新的访问令牌。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())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10016 次 |
| 最近记录: |