使用python(和一般授权)通过API访问LinkedIn数据

时间:2018-07-05 15:30:01

标签: python python-3.x oauth-2.0 linkedin linkedin-api

我正在尝试通过API访问LinkedIn数据(我没有应用程序,我只想访问公司数据-或查看可以访问的内容)。关于此主题,这里还有其他问题,但是大多数问题已经过时了(使用LinkedIn当前授权流程之前的packagaes)。

我遵循了有关授权的LinkedIn文档:https://developer.linkedin.com/docs/oauth2

我创建了一个应用程序(由于我没有网站,所以使用了无用的网站网址)。这给了我一个客户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']

此链接将我带到“授予许可”的网站,然后显示密码。使用此引脚(此处称为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请求中使用的令牌已被撤消。”此响应具有401代码。

潜在的问题是:

  • 我真的不了解API如何工作,如何与python一起工作,授权如何工作或如何知道我需要的api网址。

在相关情况下,我会进行网络抓取(使用请求和漂亮的汤来解析),但没有使用API​​。

1 个答案:

答案 0 :(得分:4)

我最终解决了这个问题,以防万一有人来此发布。在花时间之前,我还发现免费提供的API现在仅允许您访问自己的个人资料或公司页面。因此,您可以编写一个允许用户发布到自己页面的应用程序,但不能编写任何东西来获取数据。看到这里:

LinkedIn API unable to view _any_ company profile

无论如何,要使受限的API正常工作,您需要:

  • 创建一个LinkedIn帐户,创建一个应用程序,并将重定向URL添加到您的应用程序页面(我使用http://localhost:8000)。该文档说明了如何设置应用:https://developer.linkedin.com/docs/oauth2
  • 按照上述链接中的步骤进行操作,但是在python中,您请求获取“访问代码”。

    size_t

  • 打印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'})以获得巨大的链接-单击它。系统将要求您登录并允许访问,然后将您重定向到您的重定向URL。那里什么也没有,但是url末尾会有一个很长的“访问代码”。将其取出并发送到Post请求的LinkedIn中:

    html.url

  • 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})将包含“ access_token”。这是访问API所需要的。例如访问您自己的个人资料:

    token.content

    headers = {'x-li-format': 'json', 'Content-Type': 'application/json'} params = {'oauth2_access_token': access_token}

希望这对从头开始的人很有用,信息大部分都在那儿,但是有许多假定的步骤(例如如何在请求中使用访问令牌)。