2015-12-05 134 views
-1

我正嘗試在Flask應用中使用Spotify Web API授權。我能夠訪問授權頁面,但是一旦我登錄,我就會被導向KeyError請求使用Python/Flask中的Spotify API從請求令牌獲取令牌

這是我的代碼:

# Refresh and Request 

refresh = { 
    "grant_type": "refresh_token" 
    "refresh_token": "refresh_token" 
} 

refresh_post_request = requests.post(SPOTIFY_TOKEN_URL, data=refresh, 
headers=headers) 
refresh_data = json.loads(refresh_post_request.text) 
the_refresh_token = refresh_data["access_token"] 
user.spotify_refresh_token = the_refresh_token 
db.session.commit() 

SPOTIFY_TOKEN_URL和報頭前面定義。我正在使用Spotify Web Auth guidelines,但我的KeyError一直指向「access_token」。

+0

試着打印'refresh_data'。 'access_token'你認爲它在哪裏? – davidism

回答

0

您沒有將所需參數code傳遞到api端點,如鏈接的文檔中所述(步驟4)。您可以在嘗試訪問密鑰之前打印出JSON數據,以查看返回的內容,這可能是描述問題的錯誤消息。

  • code:必填。從初始請求返回到賬戶的/authorize端點的授權碼。