2016-06-29 100 views
1

我试图使用Linkedin API检索访问令牌。我使用用于python的linkedin软件包。Linkedin API:无法使用Python中的Linkedin API获取访问令牌

from linkedin import linkedin 

API_KEY = '*****' 

API_SECRET = '******' 

RETURN_URL = 'http://localhost:8080/hello' #(this works ok) 


authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL) 

print authentication.authorization_url # open this url on your browser 
application = linkedin.LinkedInApplication(authentication) 

authentication.authorization_code = 'the code obtained from login in in previous link' 

authentication.get_access_token() 

我收到以下错误:

--------------------------------------------------------------------------- 
LinkedInError        Traceback (most recent call last) 
<ipython-input-26-a0063ada08a2> in <module>() 
----> 1 authentication.get_access_token() 

C:\Users\soledad.galli\AppData\Local\Continuum\Anaconda2\lib\site-packages\linkedin\linkedin.py in get_access_token(self, timeout) 
113    'client_secret': self.secret} 
114   response = requests.post(self.ACCESS_TOKEN_URL, data=qd, timeout=timeout, verify=False) 
--> 115   raise_for_error(response) 
116   response = response.json() 
117   self.token = AccessToken(response['access_token'], response['expires_in']) 

C:\Users\soledad.galli\AppData\Local\Continuum\Anaconda2\lib\site-packages\linkedin\utils.pyc in raise_for_error(response) 
61     message = '%s: %s' % (response.get('error', error.message), 
62          response.get('error_description', 'Unknown Error')) 
---> 63     raise LinkedInError(message) 
64    else: 
65     raise LinkedInError(error.message) 


LinkedInError: invalid_request: missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : authorization code not found 

我看过其他地方,这可能是因为访问令牌有一个寿命很短,但我设法复制并很快贴并仍然有相同的错误。任何想法为什么或如何解决这个问题?

非常感谢

回答

1

我有同样的问题,它证明了我是粘贴错误的授权码。我不认为它能够这么快就为了不能够使用,但只要你我改变了代码,以便能够将其粘贴在能够使用它过期:

authentication.authorization_url # open this url on your browser 
print authentication.authorization_url 
application = linkedin.LinkedInApplication(authentication) 
code = raw_input("code = ") 
authentication.authorization_code = code 
print authentication.get_access_token() 

在我的情况redirect_uri是在本地主机上,我在web根目录中有一个index.php,它打印出你需要粘贴到python脚本中的代码。 index.php是这样的:

echo htmlspecialchars($_GET["code"]); 

希望这会对你有用。在为自己的解决方案奋斗之后,它变得不那么复杂。

相关问题