python-linkedin api-如何使用它? [英] python-linkedin api - how do I use it?

查看:93
本文介绍了python-linkedin api-如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,有关此问题的问题曾经被问过,但我找不到解决方案.我试图通过所谓的简单使用python-linkedin库访问我的LinkedIn帐户,但无法做到.根据Ozgur的页面 https://github.com/ozgur/python-linkedin ,我应该能够来打开从.authorization_url函数生成的链接,但是该链接不起作用,因为我收到一条错误消息,即使我已在LinkedIn开发人员页面的应用程序中输入了重定向链接,但该链接还是错误的. IE.尝试打开.authorization_url函数提供的链接时,浏览器中显示的是以下错误消息:

I know, questions regarding this have been asked before but I can´t find a solution. I Am trying to access my LinkedIn account through the supposedly simple to use python-linkedin library but cannot do it. According to Ozgur's page https://github.com/ozgur/python-linkedin I should be able to open the link generated from the .authorization_url function but that doesn´t work as I get an error saying that my redirect link is wrong even though I have entered it in my application at LinkedIn's developer page. I.e. when trying to open the link that the .authorization_url function gives, what shows up in the browser is the following error message:

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

"invalid redirect_uri. This value must match a URL registered with the API Key."

我期望的是一个可以批准访问我的帐户的页面.如下面的代码所示,我可以将localhost:8000(如Ozgur的页面所示)用作重定向链接,或者它必须是哪种链接?可以吗?

What I´m expecting is a page where I can approve access to my account. Can I, as in the code below have localhost:8000 (as Ozgur's page shows) as redirect link or what kind of link does it have to be? Can it be whatever?

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需要四个不同的密钥,这些密钥通常指的是:

One more question, the above refers to using Oauth2, but it should still be possible to use Oauth1 according to their developers page, and not yet deprecated. However, for using Oauth1 one needs four different keys, the ones mostly referred to:

CONSUMER_KEY,CONSUMER_SECRET,USER_TOKEN,USER_SECRET

CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_SECRET

但是,在应用程序页面(即一个用于注册应用程序的LinkedIn的页面)上,我只能找到两个:ClientID和Client_SECRET,用于Oauth2.这是否意味着仍然无法使用oauth1?

However from the application page (i.e. LinkedIn's, where one registeres the application) I can only find two: ClientID and Client_SECRET, which are for Oauth2. Does that mean it is not possible to use oauth1 anyway?

推荐答案

U仅需要ClientID and Client_SECRET.以下代码将帮助您获取其他两个重要密钥. 访问令牌密钥的有效期为60天. 无论如何都要使用ouath2, 我选择的重定向URL是" http://localhost:3000/auth/linkedin/callback "

U only need ClientID and Client_SECRET. The following code shall help u to get other two important keys. The access token keys shall be valid for 60 days. Use ouath2 anyway, The redirect url i choose is '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."

这篇关于python-linkedin api-如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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