使用 python 通过 API 访问 LinkedIn 数据(以及一般授权)
Accessing LinkedIn data via API using python (and authorisation in general)
我正在尝试通过 API 访问 LinkedIn 数据(我没有应用程序,我只想访问公司数据 - 或者查看可以访问的内容)。这里还有关于此主题的其他问题,但大多数都已过时(使用在 LinkedIn 当前授权过程之前的 packagaes)。
我关注了关于授权的 LinkedIn 文档:https://developer.linkedin.com/docs/oauth2
我创建了一个应用程序(使用无意义的网站 url,因为我没有网站)。这给了我一个客户端 ID 和客户端密码。
使用来自 LinkedIn 的(过时的)资料 (https://github.com/linkedin/api-get-started/blob/master/python/tutorial.py) 我写道:
import oauth2 as oauth
import urllib.parse as urlparse
consumer_key = 'my client id e.g. sjd6ffdf6262d'
consumer_secret = 'my customer secret e.g. d77373hhfh'
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resp,content = client.request(request_token_url, "POST")
request_token = dict(urlparse.parse_qsl(content))
clean_request_token = {}
for key in request_token.keys():
clean_request_token[key.decode('ascii')] = request_token[key].decode('ascii')
request_token = clean_request_token
print ("Go to the following link in your browser:")
print ("%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']
此 link 将我带到我 'give permission' 的网站,然后显示了个人识别码。使用此引脚(此处称为 oauth_verifier):
oauth_verifier = 12345
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
content = client.request(access_token_url,"POST")
access_token = dict(urlparse.parse_qsl(content[1]))
clean_access_token = {}
for key in access_token.keys():
clean_access_token[key.decode('ascii')] = access_token[key].decode('ascii')
access_token = clean_request_token
token = oauth.Token(key=access_token['oauth_token'],secret=access_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
response = client.request("http://api.linkedin.com/v1/companies/barclays")
由于"The token used in the OAuth request has been revoked."
,此响应有一个 401 代码
潜在的问题是:
- 我真的不明白 API 是如何工作的,他们如何与 python 一起工作,授权是如何工作的,或者如何知道我需要的 api url .
在相关的情况下,我有 web scraping 的经验(使用请求加漂亮的汤来解析)但没有 APIs.
我最终解决了这个问题,post在这里以防万一有人来这里。在您投入时间之前,我还发现免费提供的 API 现在只允许您访问您自己的个人资料或公司页面。因此,您可以编写一个允许用户 post 到他们自己的页面的应用程序,但您不能编写一些东西来获取数据。看这里:
无论如何,要使有限的 API 正常工作,您需要:
- 创建一个 LinkedIn 帐户,创建一个应用程序并将重定向 URL 添加到您的应用程序页面(我使用 http://localhost:8000). This doc says how to set up the app: https://developer.linkedin.com/docs/oauth2
按照上述 link 中的步骤,但在 python 中,您请求获得 "access code".
html = requests.get("https://www.linkedin.com/oauth/v2/authorization",
params = {'response_type':'code','client_id':client_id,
'redirect_uri':'http://localhost:8000',
'state':'somestring'})
打印 html.url
得到一个巨大的 link - 点击它。您将被要求登录并允许访问,然后您将被重定向到您的重定向 url。那里什么都没有,但是 url 的末尾会有一个很长的 "access code"。将其取出并通过 Post 请求将其发送到 LinkedIn:
token = requests.post('https://www.linkedin.com/oauth/v2/accessToken',
data = {'grant_type':'authorization_code','code':access_code,
'redirect_uri':'http://localhost:8000',
'client_id':client_id,'client_secret':client_secret})
token.content
将包含一个 "access_token"。这是访问 API 所需要的。例如访问您自己的个人资料:
headers = {'x-li-format': 'json', 'Content-Type': 'application/json'}
params = {'oauth2_access_token': access_token}
html = requests.get("https://api.linkedin.com/v1/people/~",headers=headers,params = params)
希望这对从头开始的人有用,信息大部分都在那里,但有很多假设步骤(比如如何在请求中使用访问令牌)。
我正在尝试通过 API 访问 LinkedIn 数据(我没有应用程序,我只想访问公司数据 - 或者查看可以访问的内容)。这里还有关于此主题的其他问题,但大多数都已过时(使用在 LinkedIn 当前授权过程之前的 packagaes)。
我关注了关于授权的 LinkedIn 文档:https://developer.linkedin.com/docs/oauth2
我创建了一个应用程序(使用无意义的网站 url,因为我没有网站)。这给了我一个客户端 ID 和客户端密码。
使用来自 LinkedIn 的(过时的)资料 (https://github.com/linkedin/api-get-started/blob/master/python/tutorial.py) 我写道:
import oauth2 as oauth
import urllib.parse as urlparse
consumer_key = 'my client id e.g. sjd6ffdf6262d'
consumer_secret = 'my customer secret e.g. d77373hhfh'
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resp,content = client.request(request_token_url, "POST")
request_token = dict(urlparse.parse_qsl(content))
clean_request_token = {}
for key in request_token.keys():
clean_request_token[key.decode('ascii')] = request_token[key].decode('ascii')
request_token = clean_request_token
print ("Go to the following link in your browser:")
print ("%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']
此 link 将我带到我 'give permission' 的网站,然后显示了个人识别码。使用此引脚(此处称为 oauth_verifier):
oauth_verifier = 12345
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
content = client.request(access_token_url,"POST")
access_token = dict(urlparse.parse_qsl(content[1]))
clean_access_token = {}
for key in access_token.keys():
clean_access_token[key.decode('ascii')] = access_token[key].decode('ascii')
access_token = clean_request_token
token = oauth.Token(key=access_token['oauth_token'],secret=access_token['oauth_token_secret'])
client = oauth.Client(consumer, token)
response = client.request("http://api.linkedin.com/v1/companies/barclays")
由于"The token used in the OAuth request has been revoked."
,此响应有一个 401 代码潜在的问题是:
- 我真的不明白 API 是如何工作的,他们如何与 python 一起工作,授权是如何工作的,或者如何知道我需要的 api url .
在相关的情况下,我有 web scraping 的经验(使用请求加漂亮的汤来解析)但没有 APIs.
我最终解决了这个问题,post在这里以防万一有人来这里。在您投入时间之前,我还发现免费提供的 API 现在只允许您访问您自己的个人资料或公司页面。因此,您可以编写一个允许用户 post 到他们自己的页面的应用程序,但您不能编写一些东西来获取数据。看这里:
无论如何,要使有限的 API 正常工作,您需要:
- 创建一个 LinkedIn 帐户,创建一个应用程序并将重定向 URL 添加到您的应用程序页面(我使用 http://localhost:8000). This doc says how to set up the app: https://developer.linkedin.com/docs/oauth2
按照上述 link 中的步骤,但在 python 中,您请求获得 "access code".
html = requests.get("https://www.linkedin.com/oauth/v2/authorization", params = {'response_type':'code','client_id':client_id, 'redirect_uri':'http://localhost:8000', 'state':'somestring'})
打印
html.url
得到一个巨大的 link - 点击它。您将被要求登录并允许访问,然后您将被重定向到您的重定向 url。那里什么都没有,但是 url 的末尾会有一个很长的 "access code"。将其取出并通过 Post 请求将其发送到 LinkedIn:token = requests.post('https://www.linkedin.com/oauth/v2/accessToken', data = {'grant_type':'authorization_code','code':access_code, 'redirect_uri':'http://localhost:8000', 'client_id':client_id,'client_secret':client_secret})
token.content
将包含一个 "access_token"。这是访问 API 所需要的。例如访问您自己的个人资料:headers = {'x-li-format': 'json', 'Content-Type': 'application/json'} params = {'oauth2_access_token': access_token}
html = requests.get("https://api.linkedin.com/v1/people/~",headers=headers,params = params)
希望这对从头开始的人有用,信息大部分都在那里,但有很多假设步骤(比如如何在请求中使用访问令牌)。