2014-05-19 24 views
0

我試圖從Python訪問OneDrive API。 我得到授權碼:重新啓動後,授權碼無法使用OneDrive

url = 'https://login.live.com/oauth20_authorize.srf?client_id=000000004811DD8C&scope=wl.signin%20wl.basic&response_type=code&redirect_uri=http://samplewebsite/sample_page.php' 

webbrowser.open(URL)

然後我得到的訪問和身份驗證令牌:

code = raw_input('Enter the code that appeared on the page: ') 
url = 'https://login.live.com/oauth20_token.srf' 
params = urllib.urlencode({ 
'client_id': 'CLIENT_ID', 
'redirect_uri': 'http://samplewebsite/sample_page.php', 
'client_secret': 'CLIENT_SECRET', 
'code': code, 
'grant_type': 'authorization_code' 
}) 
response = urllib2.urlopen(url, params).read() 
data = json.loads(response) 

之後,我執行一些代碼,它工作正常。 但是,在關閉應用程序後,嘗試使用相同的授權碼運行它時,會引發異常。問題是什麼?或者每次運行程序時都必須獲得新的認證碼?

+0

您可以加入關於你的異常的詳細信息看見了什麼? – ginach

+0

以下是我嘗試再次使用授權密鑰時所獲得的回溯。第20行是response = urllib2.urlopen(url,params).read() https://www.dropbox.com/s/94jqwtzxueh3amm/shot_140519_204632.png – vadimmelnyk

回答

1

授權碼在1小時後過期。這聽起來像,你的情況,你需要使用刷新令牌。查看我們的支持OAuth 2.0流程的下列文件:

http://msdn.microsoft.com/en-us/library/dn631818.aspx

以下服務器端身份驗證的情況也可能會有所幫助:

https://github.com/liveservices/LiveSDK-for-Windows/tree/master/src/Web/Samples/OAuthServer

+0

什麼時候刷新標記過期? –

+0

我在這些鏈接中沒有看到任何服務器端身份驗證 –