2016-06-29 103 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"]); 

希望這會對你有用。在爲自己的解決方案奮鬥之後,它變得不那麼複雜。

相關問題