gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:09:10 24 4
gpt4 key购买 nike

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

“无效的 redirect_uri。此值必须与使用 API key 注册的 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,需要四个不同的 key ,主要指的是:

CONSUMER_KEY、CONSUMER_SECRET、USER_TOKEN、USER_SECRET

但是从应用程序页面(即 LinkedIn 的,在那里注册应用程序)我只能找到两个:ClientID 和 Client_SECRET,它们用于 Oauth2。这是否意味着无论如何都无法使用 oauth1?

最佳答案

你只需要ClientID 和Client_SECRET。以下代码将帮助您获得另外两个重要的 key 。访问 token key 的有效期为 60 天。无论如何使用ouath2,我选择的重定向 url 是 ' 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 - 我该如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com