<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: web API Forbidden 403 on PUT/DELETE Queue in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655430#M11186</link>
    <description>&lt;P&gt;You have likely generated a token using Client Credentials, but to access and edit user data, you will need to generate a token through the &lt;A href="https://developer.spotify.com/documentation/web-api/tutorials/code-flow" target="_blank" rel="noopener"&gt;Authorization Code Flow&lt;/A&gt;.&lt;/P&gt;</description>
    <pubDate>Sat, 21 Oct 2023 07:42:34 GMT</pubDate>
    <dc:creator>Ximzend</dc:creator>
    <dc:date>2023-10-21T07:42:34Z</dc:date>
    <item>
      <title>web API Forbidden 403 on PUT/DELETE Queue</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655263#M11184</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Plan&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Free&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I successfully generated token for API and can make a POST requests with curl from my local machine on win10&lt;BR /&gt;and from my dedicated linux server without problems&lt;BR /&gt;for example:&lt;BR /&gt;curl -X GET &lt;A href="https://api.spotify.com/v1/playlists/3cEYpjA9oz9GiPac4AsH4n" target="_blank" rel="noopener"&gt;https://api.spotify.com/v1/playlists/**********&lt;/A&gt; -H "Authorization: Bearer ***HASH***"&lt;/P&gt;&lt;P&gt;but when I trt to make PUT/DELETE requests for(Save Tracks for Current User) current user's 'Your Music' library like this&lt;/P&gt;&lt;P&gt;curl -X PUT &lt;A href="https://api.spotify.com/v1/me/tracks?ids=7ouMYWpwJ422jRcDASZB7P%2C4VqPOruhp5EdPBeR92t6lQ%2C2takcwOaAZWiXQijPHIx7B" target="_blank" rel="noopener"&gt;https://api.spotify.com/v1/me/tracks?ids=7ouMYWpwJ422jRcDASZB7P%2C4VqPOruhp5EdPBeR92t6lQ%2C2takcwOaAZWiXQijPHIx7B&lt;/A&gt; -H "Authorization: Bearer ***HASH***"&lt;/P&gt;&lt;P&gt;i got error:&lt;/P&gt;&lt;P&gt;"error" : {&lt;BR /&gt;"status" : 403,&lt;BR /&gt;"message" : "Forbidden."&lt;BR /&gt;}&lt;BR /&gt;If I executing this from page&lt;BR /&gt;&lt;A href="https://developer.spotify.com/documentation/web-api/reference/save-albums-user" target="_blank" rel="noopener"&gt;https://developer.spotify.com/documentation/web-api/reference/save-albums-user&lt;/A&gt;&lt;BR /&gt;it's working fine&lt;/P&gt;&lt;P&gt;Why this happens? my API key have not right permissions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 21:24:28 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655263#M11184</guid>
      <dc:creator>1u</dc:creator>
      <dc:date>2023-10-24T21:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: web API Forbidden 403 on PUT/DELETE Queue</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655430#M11186</link>
      <description>&lt;P&gt;You have likely generated a token using Client Credentials, but to access and edit user data, you will need to generate a token through the &lt;A href="https://developer.spotify.com/documentation/web-api/tutorials/code-flow" target="_blank" rel="noopener"&gt;Authorization Code Flow&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Oct 2023 07:42:34 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655430#M11186</guid>
      <dc:creator>Ximzend</dc:creator>
      <dc:date>2023-10-21T07:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: web API Forbidden 403 on PUT/DELETE Queue</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655978#M11195</link>
      <description>&lt;P&gt;Thanks for advice but I still getting 403,"message" : "Forbidden." even after I generated access tokens from "code flow"&lt;BR /&gt;&lt;BR /&gt;Here is my walkthrough &lt;A href="https://developer.spotify.com/documentation/web-api/tutorials/code-flow" target="_blank" rel="noopener nofollow noreferrer"&gt;Authorization Code Flow&lt;/A&gt; scheme in python :&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1u_0-1698006130902.png" style="width: 999px;"&gt;&lt;img src="https://community.spotify.com/t5/image/serverpage/image-id/164303iA388557C71D0EC26/image-size/large?v=v2&amp;amp;px=999" role="button" title="1u_0-1698006130902.png" alt="1u_0-1698006130902.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;so for running I use 3 python files:&lt;BR /&gt;&lt;STRONG&gt;spo.py&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;refresh.py&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;access.py&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;Go to dir where spo.py file is located and run this in linux console:&lt;BR /&gt;&lt;STRONG&gt;uvicorn spo:app --host 0.0.0.0 --port 8000 2&amp;gt;&amp;amp;1 | tee -a uvicorn_access.log&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then open in browser &lt;A href="http://localhost:8000" target="_blank" rel="noopener"&gt;&lt;STRONG&gt;http://localhost:8000&lt;/STRONG&gt;&lt;/A&gt;&amp;nbsp; complete autorisation wih your log&amp;amp;pass and then click on Authorize link&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;after that run once in console:&lt;STRONG&gt;&lt;BR /&gt;python3 refresh.py&lt;BR /&gt;&lt;/STRONG&gt;and finally&lt;BR /&gt;&lt;STRONG&gt;python3 access.py&lt;/STRONG&gt;&lt;BR /&gt;access_tokenS will be in accessT.txt&lt;BR /&gt;it have rights permissions as I can see&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1u_1-1698008269251.png" style="width: 999px;"&gt;&lt;img src="https://community.spotify.com/t5/image/serverpage/image-id/164305iDFBD3BE854553123/image-size/large?v=v2&amp;amp;px=999" role="button" title="1u_1-1698008269251.png" alt="1u_1-1698008269251.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;after that I'm trying some &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;API requests&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;:&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;A href="https://developer.spotify.com/documentation/web-api/reference/add-tracks-to-playlist" target="_blank" rel="noopener"&gt;add-tracks-to-playlist&lt;/A&gt; : (WORKING)&lt;STRONG&gt;&lt;BR /&gt;curl -X POST '&lt;A href="https://api.spotify.com/v1/playlists/3NbqIBJy3TdDFWWxFupNlN/tracks?uris=spotify%3Atrack%3A7hkYSZ1Ry7OeBGrTzLhg0c" target="_blank" rel="noopener"&gt;https://api.spotify.com/v1/playlists/*****/tracks?uris=spotify%3Atrack%3A7hkYSZ1Ry7OeBGrTzLhg0c&lt;/A&gt;' -H "Authorization: Bearer $(tail -n 1 /PATH/accessT.txt)"&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;A href="https://developer.spotify.com/documentation/web-api/reference/save-tracks-user" target="_self"&gt;save-tracks-user&lt;/A&gt; : (&lt;FONT color="#FF0000"&gt;NOT WORKING&lt;/FONT&gt;)&lt;STRONG&gt;&lt;BR /&gt;curl --request PUT '&lt;A href="https://api.spotify.com/v1/me/tracks?ids=7hkYSZ1Ry7OeBGrTzLhg0c" target="_blank" rel="noopener"&gt;https://api.spotify.com/v1/me/tracks?ids=7hkYSZ1Ry7OeBGrTzLhg0c&lt;/A&gt;' --header "Authorization: Bearer $(tail -n 1 /PATH/accessT.txt)"&lt;BR /&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;getting 403,"message" : "Forbidden. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;If I intentionally corrupt the token, it says the token is invalid&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;So &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;could it be related to my free plan&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; or not?&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;also I'm tryed access tokens*1 and *2 with same result it can see on my scheme upper&lt;BR /&gt;can anybody help me?&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;here is my &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;source code files&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;:&lt;STRONG&gt;&lt;BR /&gt;spo.py&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import requests
import uvicorn

client_id = "99493f576c2d43018***************"
client_secret = "8debd3e6994b4***************"
redirect_uri = "http://localhost:8000" # e.g. http://localhost:8000/callback/ --&amp;gt; you will have to whitelist this url in the spotify developer dashboard 

app = FastAPI()
def get_access_token(auth_code: str):
    response = requests.post(
        "https://accounts.spotify.com/api/token",
        data={
            "grant_type": "authorization_code",
            "code": auth_code,
            "redirect_uri": redirect_uri,
	    # not work"state: state": state
        },
        auth=(client_id, client_secret),
    )
    access_token = response.json()["access_token"]
    return {"Authorization": "Bearer "**bleep**("/")
async def auth():
    scope = ["playlist-modify-private", "playlist-modify-public"]  #Permissions
    auth_url = f"https://accounts.spotify.com/authorize?response_type=code&amp;amp;client_id={client_id}&amp;amp;redirect_uri={redirect_uri}&amp;amp;scope={' '.join(scope)}"
    return HTMLResponse(content=f'&amp;lt;a href="{auth_url}"&amp;gt;Authorize&amp;lt;/a&amp;gt;')
#if __name__ == "__main__":
#    uvicorn.run(app)
#RUN in console =&amp;gt;&amp;gt; uvicorn spo:app --host 0.0.0.0 --port 8000 2&amp;gt;&amp;amp;1 | tee -a uvicorn_access.log&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;refresh.py&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
import requests
import base64
import json

# Spotify API credentials
client_id = "99493f576c2d430******"
client_secret = "8debd3e6994******"
redirect_uri = "http://localhost:8000"
# Define the URL for the /api/token endpoint
token_url = "https://accounts.spotify.com/api/token"

#Read last line from uvicorn_access.log for hash CODE
###########################################################
with open("/PATH/uvicorn_access.log", "r") as log_file:
    # Read the last line from the log file
    lines = log_file.read().splitlines()
    last_line = lines[-1]

    # Use regular expression to find the code in the last line
    code_match = re.search(r'code=([^&amp;amp;\s"]+)', last_line)

    if code_match:
        code = code_match.group(1)
        #print(f"{code}")
    else:
        print("Code not found in the last line.")
###########################################################
# Prepare the data for the request body
data = {
    #"code": "TEST_HASH",
    "code": code, # from uvicorn_access.log
    "redirect_uri": redirect_uri,
    "grant_type": "authorization_code"
}

encoded = base64.b64encode((client_id + ":" + client_secret).encode("ascii")).decode("ascii")
# Prepare the headers for the request
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic " + encoded
}

# Send the POST request
response = requests.post(token_url, data=data, headers=headers)

# Check the response
if response.status_code == 200:
    response_data = response.json()
    print("Full JSON Response:")
    print(json.dumps(response_data, indent=4))
    refresh_token = response_data["refresh_token"]
    access_token = response_data["access_token"]
######SAVE TO LOGS
    # Append the refresh_token to the end of the log file
    with open("/PATH/uvicorn_access.log", "a") as log_file:
        log_file.write(f"refresh_token: {refresh_token}\n")

    # Append the access_tokenS to the end of the log file
    with open("/PATH/accessT.txt", "a") as log_file:
        log_file.write(f"{access_token}\n")
#########
else:
    print(f"Error: {response.status_code}, {response.text}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;access.py&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
import requests
import base64
import json

# Spotify API credentials
client_id = "99493f576c2d43***********"
client_secret = "8debd3e699***********"
# Define the URL for the /api/token endpoint
token_url = "https://accounts.spotify.com/api/token"

#Read last line from uvicorn_access.log for hash of refresh_token
###########################################################
with open("/PATH/uvicorn_access.log", "r") as log_file:
    # Read the last line from the log file
    lines = log_file.read().splitlines()
    last_line = lines[-1]

    # Use regular expression to find the code in the last line
    token_match = re.search(r'refresh_token: ([^\s]+)', last_line)

    if token_match:
        refresh_token = token_match.group(1)
        #print(f"{refresh_token}")
    else:
        print("Refresh Token not found in the last line.")
###########################################################
# Prepare the data for the request body
data = {
    "grant_type": "refresh_token",
    "refresh_token": refresh_token, # from uvicorn_access.log
}

encoded = base64.b64encode((client_id + ":" + client_secret).encode("ascii")).decode("ascii")
# Prepare the headers for the request
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic " + encoded
}

# Send the POST request
response = requests.post(token_url, data=data, headers=headers)

if response.status_code == 200:
    response_data = response.json()
    print("Full JSON Response:")
    print(json.dumps(response_data, indent=4))
    access_token = response_data["access_token"]
    #print(f"Access Token: {access_token}")

    # Append the access_tokenS to the end of the log file
    with open("/PATH/accessT.txt", "a") as log_file:
        log_file.write(f"{access_token}\n")
else:
    print(f"Error: {response.status_code}, {response.text}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 21:21:24 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5655978#M11195</guid>
      <dc:creator>1u</dc:creator>
      <dc:date>2023-10-24T21:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: web API Forbidden 403 on PUT/DELETE Queue</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5656969#M11232</link>
      <description>&lt;P&gt;I'm find out&lt;BR /&gt;for edit liked songs library need to use SCOPE:&lt;STRONG&gt;user-library-modify&lt;BR /&gt;&lt;/STRONG&gt;everything working now!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Oct 2023 22:38:02 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/web-API-Forbidden-403-on-PUT-DELETE-Queue/m-p/5656969#M11232</guid>
      <dc:creator>1u</dc:creator>
      <dc:date>2023-10-24T22:38:02Z</dc:date>
    </item>
  </channel>
</rss>

