使用PYTHON通过API访问LinkedIn数据(和一般授权) [英] Accessing LinkedIn data via API using python (and authorisation in general)

查看:36
本文介绍了使用PYTHON通过API访问LinkedIn数据(和一般授权)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我遵循了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代码。使用此PIN(这里称为OAUTH_VERIMER):

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 URL。

在相关的情况下,我有过Web抓取的经验(使用请求和漂亮的汤来解析),但没有使用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'})

    /li>
  • 打印html.url要获得一个巨大的链接-请单击它。你会被要求登录并允许访问,然后你会被重定向到你的重定向URL。这里什么都没有,但URL的末尾会有一个很长的"访问码"。把这个拿出来,用一个帖子请求发送到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)

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

这篇关于使用PYTHON通过API访问LinkedIn数据(和一般授权)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆