python-linkedin api - 我该如何使用它?

时间:2015-07-17 17:15:54

标签: python linkedin

我知道,之前已经提出过有关此问题,但我找不到解决办法。我试图通过所谓的简单易用的python-linkedin库访问我的LinkedIn帐户,但不能这样做。根据Ozgur的页面https://github.com/ozgur/python-linkedin,我应该能够打开从.authorization_url函数生成的链接,但这不起作用,因为我收到错误,说我的重定向链接错误,即使我已经在我的应用程序中输入了它在LinkedIn的开发者页面上。即当试图打开.authorization_url函数给出的链接时,浏览器中显示的内容是以下错误消息:

“无效的redirect_uri。此值必须与使用API​​密钥注册的URL匹配。”

我期待的是一个我可以批准访问我帐户的页面。我可以在下面的代码中将localhost:8000(如Ozgur的页面所示)作为重定向链接或它必须是什么类型的链接?它可以是什么?

from linkedin import linkedin
import webbrowser

API_KEY = '********'
API_SECRET = '*******'
RETURN_URL = 'http://localhost:8000'

authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values())
print (authentication.authorization_url)  # open this url on your browser
webbrowser.open(authentication.authorization_url)
application = linkedin.LinkedInApplication(authentication)
authentication.authorization_code = '4CqONljAz622ZBo0'
authentication.get_access_token()

我该怎么做?

还有一个问题,上面提到的是使用Oauth2,但仍然可以根据开发者页面使用Oauth1,但尚未弃用。但是,对于使用Oauth1,需要四个不同的密钥,主要是:

CONSUMER_KEY,CONSUMER_SECRET,USER_TOKEN,USER_SECRET

然而,从应用程序页面(即LinkedIn,其中一个注册应用程序)我只能找到两个:ClientID和Client_SECRET,它们用于Oauth2。这是否意味着无论如何都不可能使用oauth1?

3 个答案:

答案 0 :(得分:10)

你只需要ClientID and Client_SECRET。以下代码将帮助您获得其他两个重要的密钥。 访问令牌密钥有效期为60天。 无论如何,请使用ouath2, 我选择的重定向网址是' http://localhost:3000/auth/linkedin/callback'

检查出来

import oauth2 as oauth
import urlparse

consumer_key           = "******"
consumer_secret        = "******"
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)

request_token_url      = 'https://api.linkedin.com/uas/oauth/requestToken'
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200':
    raise Exception("Invalid response %s." % resp['status'])

print content
print "\n"

request_token = dict(urlparse.parse_qsl(content))

print "Requesr Token:",  "\n"
print "- oauth_token        = %s" % request_token['oauth_token'], "\n"
print "- oauth_token_secret = %s" % request_token['oauth_token_secret'], "\n"

authorize_url = 'https://api.linkedin.com/uas/oauth/authorize'
print "Go to the following link in your browser:", "\n"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']), "\n"

accepted = 'n'
while accepted.lower() == 'n':
    accepted = raw_input('Have you authorized me? (y/n) ')
oauth_verifier = raw_input('What is the PIN? ')

access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)

resp, content = client.request(access_token_url, "POST")
access_token = dict(urlparse.parse_qsl(content))

print "Access Token:", "\n"
print "- oauth_token        = %s" % access_token['oauth_token'], "\n"
print "- oauth_token_secret = %s" % access_token['oauth_token_secret']
print "You may now access protected resources using the access tokens above."

答案 1 :(得分:3)

针对Python 3进行了更新:

#%%### 3. LinkedIn API Work

import oauth2 as oauth
import urllib

consumer_key='XXXXXX' #from Linkedin site
consumer_secret='XXXXXX' #from Linkedin site
consumer=oauth.Consumer(consumer_key, consumer_secret)
client=oauth.Client(consumer)

request_token_url='https://api.linkedin.com/uas/oauth/requestToken'
resp, content=client.request(request_token_url, "POST")
if resp['status']!='200':
    raise Exception("Invalid response %s." % resp['status'])
content_utf8=str(content,'utf-8') #convert binary to utf-8 string
request_token=dict(urllib.parse.parse_qsl(content_utf8))
authorize_url=request_token['xoauth_request_auth_url']

print("Go to the following link in your browser:", "\n")
print(authorize_url+'?oauth_token='+request_token['oauth_token'])

accepted='n'
while accepted.lower()=='n':
    accepted=input('Have you authorized me? (y/n)') #prompt for input (y)
oauth_verifier=input('What is the PIN?') #prompt for pin

access_token_url='https://api.linkedin.com/uas/oauth/accessToken'
token=oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, "POST")
content8=str(content,'utf-8')
access_token = dict(urllib.parse.parse_qsl(content8))

print("Access Token:", "\n")
print("- oauth_token        = "+access_token['oauth_token']+'\n')
print("- oauth_token_secret = "+access_token['oauth_token_secret'])
print("You may now access protected resources using the access tokens above.")

答案 2 :(得分:0)

其中许多答案似乎已经过时了。

鉴于您似乎不受特定项目的束缚,我建议您看看https://github.com/tomquirk/linkedin-api(免责声明:我维护它)。

您可以通过以下方式向LinkedIn进行身份验证:

from linkedin_api import LinkedIn

# Authenticate using any LinkedIn account credentials
api = Linkedin('youremail@gmail.com', 'l33tpassword')

在那之后,您可以继续执行请求,发送电子邮件,获取LinkedIn个人资料:

profile = api.get_profile('profile-id')