Spotify API授权+令牌密钥错误-Python3 [英] Spotify API authorization + token key error - python3

查看:252
本文介绍了Spotify API授权+令牌密钥错误-Python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题,在我的脚本中,我有些困惑.在过去的几周中,我一直在运行该脚本,没有任何问题,现在我得到了令牌的KeyError.

I'm running into this issue where with my script and I'm a bit stumped. I've been running this script with no problems for the past few weeks and now I'm getting a KeyError for the token.

这是我的代码:

# IMPORTS
import os
import re
import requests
import json
import numpy as np
import pandas as pd
import time
from pprint import pprint as pp
import datetime as dt
import sys 
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
acc_path = "../../access/"
sys.path.append(acc_path)

pd.set_option('display.max_rows', 10000)
pd.set_option('display.max_columns', 100)
pd.set_option('display.max_colwidth', -1)

# Spotify Credentials
sp_url = 'https://api.spotify.com/v1/'
client_id = os.environ.get('SPOT_CLIENT_ID')
client_secret = os.environ.get('SPOT_CLIENT_SECRET')
output_data = '/users/Desktop/file_date.csv'
spot_scopes = os.environ.get('SPOT_SCOPES')
spot_user_name = os.environ.get('SPOT_USER_NAME') # spotify account username

sp_acc = requests.post('https://accounts.spotify.com/api/token', data = {'grant_type' : 'client_credentials'}, 
                       auth = (client_id, client_secret))
sp_bear_head = {'Authorization' : 'Bearer' + str(sp_acc.json()['access_token'])}

KeyERROR

---> 31 sp_bear_head = {'Authorization' : 'Bearer' + str(sp_acc.json()['access_token'])}

KeyError: 'access_token'

我检查了bash以确保客户端ID,秘密作用域等都正确,并且可以确认这不是问题.这里的任何方向都将非常有帮助!

I checked my bash to make sure client id, secret scopes etc. are all correct and can confirm that is not the issue. Any direction here would be very helpful!

推荐答案

之所以起作用,而不是现在起作用,是由于access token到期.

The reason why it worked before and not now is due to the expiration of the access token.

令牌的到期时间由Spotify确定,因此您只需要解决它们的设置约束.

The token's expiration time is determined by Spotify so you just have to work around their set constraints.

话虽如此,您可以根据请求请求令牌(https://accounts.spotify.com/api/token)时Spotify在响应中发回的属性expires_in来预测何时需要生成/使用新令牌. expires_in属性是一个整数,它告诉您令牌将使用多少秒.如其授权文档所示, expires_in属性的返回值为3600(秒)或1小时.

That being said, you can anticipate when a new token will need to be generated/used based on the property expires_in which Spotify sends back in the response when you request a token (https://accounts.spotify.com/api/token). The expires_in property is an integer and it tells you how many seconds the token will be good for. As seen in their authorization documentation, the expires_in property is returned with the value 3600 (seconds) or, 1 hour.

该小时结束后,使用您的refresh_token请求新令牌.

After that hour is up, use your refresh_token to request a new token.

这篇关于Spotify API授权+令牌密钥错误-Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆