我正在尝试通过 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']
此链接将我带到我“授予许可”的网站,然后显示个人识别码。使用此 pin(此处称为 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")
由于“OAuth 请求中使用的 token 已被撤销”,此响应有一个 401 代码。
潜在的问题是:
- 我真的不明白 API 是如何工作的,它们如何与 python 一起工作,授权是如何工作的,或者如何知道我需要的 api url。
在相关情况下,我有网络抓取的经验(使用请求加上漂亮的汤来解析)但没有 API。
请您参考如下方法:
我最终解决了这个问题,张贴在这里以防有人来这里。在你投入时间之前,我还发现免费提供的 API 现在只允许你访问你自己的个人资料或公司页面。所以你可以写一个应用程序,允许用户在他们自己的页面上发帖,但你不能写一些东西来获取数据。看这里:
LinkedIn API unable to view _any_ company profile
无论如何,要使有限的 API 正常工作,您需要:
- 创建一个 LinkedIn 帐户,创建一个应用程序并将重定向 URL 添加到您的应用程序页面(我使用了 http://localhost:8000 )。该文档介绍了如何设置应用程序:https://developer.linkedin.com/docs/oauth2
按照上述链接中的步骤,但在 python 中,您发出请求以获取“访问代码”。
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以获得巨大的链接 - 单击它。你将被要求登录并允许访问,然后你将被重定向到你的重定向 url。那里什么也没有,但是 url 的末尾会有一个很长的“访问代码”。将其拉出并通过 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)
希望这对从头开始的人有用,信息大部分都在那里,但有很多假设步骤(比如如何在请求中使用访问 token )。
