Spotify在Python 3中刷新令牌3

发布于 2025-01-31 03:41:11 字数 1574 浏览 1 评论 0 原文

我正在尝试获得刷新令牌,并在临时访问令牌到期时使用它来请求新的访问令牌。 Spotify的文档指出响应主体应包含以下JSON数据:

{
   "access_token": "NgCXRK...MzYjw",
   "token_type": "Bearer",
   "scope": "user-read-private user-read-email",
   "expires_in": 3600,
   "refresh_token": "NgAagA...Um_SHo"
}

但是,在下面使用我的代码仅提供以下内容:

{
  "access_token": "xxxx....",
  "token_type": "Bearer",
  "expires_in": 3600
}

我使用以下功能请求访问令牌:

def get_access_token(client_id, client_secret):   # getting access token from spotify using client ID
    #  Encoding Client ID and Client Secret in Base64
    token_url = "https://accounts.spotify.com/api/token"
    client_creds = f"{client_id}:{client_secret}"
    client_creds_b64 = base64.b64encode(client_creds.encode())
    token_data = {
        "grant_type":  "client_credentials"
    }
    token_headers = {
        'Authorization': f"Basic {client_creds_b64.decode()}"
    }
    r = requests.post(token_url, data=token_data, headers=token_headers)
    valid_request = r.status_code in range(200, 299)
    token_response_data = r.json()
    # pretty print response body
    print(json.dumps(token_response_data, indent=2))    # response 200 -> successful attempt
    if valid_request:
        access_token = token_response_data['access_token']  
    return access_token

我不确定为什么响应中缺少范围和刷新令牌身体。如何更改此功能以包含这些功能?

预先感谢您的时间!

Spotify文档:

I am attempting to get a refresh token and use it to request a new access token when the temporary access token expires.
Spotify's documentation states the response body should contain the following json data:

{
   "access_token": "NgCXRK...MzYjw",
   "token_type": "Bearer",
   "scope": "user-read-private user-read-email",
   "expires_in": 3600,
   "refresh_token": "NgAagA...Um_SHo"
}

However, using my code below only provides the following:

{
  "access_token": "xxxx....",
  "token_type": "Bearer",
  "expires_in": 3600
}

I am using the following function to request an access token:

def get_access_token(client_id, client_secret):   # getting access token from spotify using client ID
    #  Encoding Client ID and Client Secret in Base64
    token_url = "https://accounts.spotify.com/api/token"
    client_creds = f"{client_id}:{client_secret}"
    client_creds_b64 = base64.b64encode(client_creds.encode())
    token_data = {
        "grant_type":  "client_credentials"
    }
    token_headers = {
        'Authorization': f"Basic {client_creds_b64.decode()}"
    }
    r = requests.post(token_url, data=token_data, headers=token_headers)
    valid_request = r.status_code in range(200, 299)
    token_response_data = r.json()
    # pretty print response body
    print(json.dumps(token_response_data, indent=2))    # response 200 -> successful attempt
    if valid_request:
        access_token = token_response_data['access_token']  
    return access_token

I am unsure why the scope and refresh token are missing from the response body. How can I alter this function to include these?

Thanks in advance for your time!

Spotify documentation: https://developer.spotify.com/documentation/general/guides/authorization/code-flow/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2025-02-07 03:41:11

更改令牌数据修复了以下操作:

    token_data = {
        "grant_type": "authorization_code",
        "code": "xxxx....",
        "redirect_uri": "your redirect uri from Spotify for devs"
     }

您需要在Spotify上为Devs设置重定向URI,然后对其进行编码以将其用作参数。

要将“代码”参数用数据填充此URL,您将被重定向到重定向URI。
https://accounts.spotify.com/authorize?client_id= your客户端ID& wendesp_type = code& redirect_uri =您编码的redirect uri&scope = playlist-modify-publist-modify-public-public%20playlist-modify-privatifify-code

>>然后,代码将在搜索栏中复制/粘贴。它应该看起来像这样:

https:// redirecturi?code = xxx .....

以下是一个很好的视频指南:

https://www.youtube.com/watch?v=-FsFT6OwE1A&ab_channel=EuanMorgan

Altering the token data fixes this:

    token_data = {
        "grant_type": "authorization_code",
        "code": "xxxx....",
        "redirect_uri": "your redirect uri from Spotify for devs"
     }

You need to set up a redirect uri on Spotify for devs, then encode it to use it as a parameter.

To get the 'code' parameter fill-out this URL with your data and you will be redirected to your redirect uri.
https://accounts.spotify.com/authorize?client_id=YOUR CLIENT ID&response_type=code&redirect_uri=YOUR ENCODED REDIRECT URI&scope=playlist-modify-public%20playlist-modify-private

The code will then be in the search bar to copy/paste. It should look something like this:

https://your redirectURI?code=xxx.....

Here is a good video guide for reference:

https://www.youtube.com/watch?v=-FsFT6OwE1A&ab_channel=EuanMorgan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文