对 linkedin 的身份验证 API
Authentification to linkedin API
我是 Python 的新手,我想测试 Linkedin API。
我从这个站点得到了一个验证码示例(使用 oauth2):https://github.com/ozgur/python-linkedin
我想我在 Linkedin 上的应用程序配置没有问题:
客户ID:XXX
秘密客户 : YYY
选中所有这些框:r_basicprofile、r_emailaddress、rw_company_admin、w_share
OAuth 2.0 => 授权 URL : http://localhost:8000
代码如下:
#-*- coding: utf-8 -*-
from linkedin import linkedin
API_KEY = 'XXX'
API_SECRET = 'YYY'
RETURN_URL = 'http://localhost:8000'
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL)
print "authentication.authorization_url : " + authentication.authorization_url
print "authentication.key : " + authentication.key
print "authentication.secret : " + authentication.secret
print "authentication.redirect_uri : " + authentication.redirect_uri
print "authentication.state : " + authentication.state
print authentication.authorization_code
print authentication.token
print authentication._error
application = linkedin.LinkedInApplication(authentication)
结果是:
authentication.authorization_url : https://www.linkedin.com/uas/oauth2/authorization?scope=&state=a2eb48d9b7b5f94a24dfbf36d498ebdc&redirect_uri=http%3A//localho
st%3A8000&response_type=code&client_id=XXX
authentication.key : XXX
authentication.secret : YYY
authentication.redirect_uri : http://localhost:8000
authentication.state : a2eb48d9b7b5f94a24dfbf36d498ebdc
None
None
None
我不明白为什么我的 authorization_code 是 None。根据 git hub link,redirect_url 应该包含 URL + 授权码。我这里只有URL,所以无法继续进行认证。
我做了一些研究,但找不到任何东西。有人知道我的代码或配置有什么问题吗?
谢谢!
嗯,终于找到问题所在了!
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL)
这个returns一个URL(例如:https://www.linkedin.com/uas/oauth2/authorization?scope=r_basicprofile%20r_emailaddress&state=4a8b5b5932f182fff0a1731ebfbb05ef&redirect_uri=http%3A//localhost%3A8000&response_type=code&client_id=XXX). I had to open this URL in my browser, to log in with my Linkedin account. Then I was redirected to this URL : http://localhost%3A8000/?code=my_code&state=31624da3ad7331c11def407de0a56cc4
my_code 是用于获取令牌的代码。
authentication.authorization_code = 'my_code'
authentication.get_access_token()
获得令牌后,我可以请求使用 API。
希望对您有所帮助。
我是 Python 的新手,我想测试 Linkedin API。 我从这个站点得到了一个验证码示例(使用 oauth2):https://github.com/ozgur/python-linkedin
我想我在 Linkedin 上的应用程序配置没有问题:
客户ID:XXX
秘密客户 : YYY
选中所有这些框:r_basicprofile、r_emailaddress、rw_company_admin、w_share
OAuth 2.0 => 授权 URL : http://localhost:8000
代码如下:
#-*- coding: utf-8 -*-
from linkedin import linkedin
API_KEY = 'XXX'
API_SECRET = 'YYY'
RETURN_URL = 'http://localhost:8000'
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL)
print "authentication.authorization_url : " + authentication.authorization_url
print "authentication.key : " + authentication.key
print "authentication.secret : " + authentication.secret
print "authentication.redirect_uri : " + authentication.redirect_uri
print "authentication.state : " + authentication.state
print authentication.authorization_code
print authentication.token
print authentication._error
application = linkedin.LinkedInApplication(authentication)
结果是:
authentication.authorization_url : https://www.linkedin.com/uas/oauth2/authorization?scope=&state=a2eb48d9b7b5f94a24dfbf36d498ebdc&redirect_uri=http%3A//localho st%3A8000&response_type=code&client_id=XXX
authentication.key : XXX
authentication.secret : YYY
authentication.redirect_uri : http://localhost:8000
authentication.state : a2eb48d9b7b5f94a24dfbf36d498ebdc
None
None
None
我不明白为什么我的 authorization_code 是 None。根据 git hub link,redirect_url 应该包含 URL + 授权码。我这里只有URL,所以无法继续进行认证。
我做了一些研究,但找不到任何东西。有人知道我的代码或配置有什么问题吗?
谢谢!
嗯,终于找到问题所在了!
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL)
这个returns一个URL(例如:https://www.linkedin.com/uas/oauth2/authorization?scope=r_basicprofile%20r_emailaddress&state=4a8b5b5932f182fff0a1731ebfbb05ef&redirect_uri=http%3A//localhost%3A8000&response_type=code&client_id=XXX). I had to open this URL in my browser, to log in with my Linkedin account. Then I was redirected to this URL : http://localhost%3A8000/?code=my_code&state=31624da3ad7331c11def407de0a56cc4
my_code 是用于获取令牌的代码。
authentication.authorization_code = 'my_code'
authentication.get_access_token()
获得令牌后,我可以请求使用 API。
希望对您有所帮助。