Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Web API Reference "Get Token" button

Web API Reference "Get Token" button

Overnight the entire web API reference has changed, and every page has been overhauled... to show the exact same thing... but aesthetically pleasing... so where is the "Get Token" button that manually generates an OAuth token for you to use to test the API, this button was plastered on every page of the reference, now it's missing? What is the alternative that doesn't involve me creating an entire code to fetch the token I had an easy access button for, considering it's an entire external authentication process?? There's a reason I generated it manually every hour I needed it. 

image.png
image.png
Reply
7 Replies

Spotify please fix this! I also prefer manually collecting the token when I want to update my playlist!!!!

I just put this together for someone else, so happy to share code for obtaining a valid token. Happy to advise if you are not familiar with Python. Plug in your client ID/client secret and when run this will print out your token, or you can use it in whatever way you please. If you do not have software installed on your computer for editing and running code, you could quickly and easily set up a replit account to run the code whenever you need a fresh token.

 

 

import requests, datetime

'''
Implementation of Token class for gaining, storing and verifying Spotify developer
tokens. Using text file to store; explore databases for production code.
'''

# Enter client ID and secret from developer console; store in .env or similar
# for production
CLIENT_ID = '' 
CLIENT_SECRET = ''

class Token:
    # Token class will try to load an existing saved token rather than
    # send unnecessary requests; at scale this would improve performance
    def __init__(self):
        self.__token = None
        self.__time = None
        self.load_token()
        try:
            if self.__time + datetime.timedelta(hours=1) < datetime.datetime.now():
                # token out of time; update
                print('Token out of time')
                self.get_token()
            else:
                print('Token up to date')

        except TypeError:
            # token time is not a datetime object; update
            print('Token time format incorrect')
            self.get_token()
        self.save_token()

    def get_token(self):
        endpoint = "https://accounts.spotify.com/api/token"
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        body = {
                "grant_type": "client_credentials",
                "client_id": CLIENT_ID,
                "client_secret": CLIENT_SECRET
            }
        response = requests.post(endpoint, headers=headers, data=body)
        self.__token = response.json()['access_token']
        self.__time = datetime.datetime.now()
        self.save_token()

    def save_token(self):
        details = self.__token + ',' + self.__time.strftime("%d-%b-%Y (%H:%M:%S.%f)")
        with open('token.txt', 'w') as file:
            file.write(details)

    def load_token(self):
        try:
            with open('token.txt', 'r') as file:
                result = file.read()
                result = result.strip().split(',')
                self.__token = result[0]
                self.__time = datetime.datetime.strptime(result[1], "%d-%b-%Y (%H:%M:%S.%f)")
        except FileNotFoundError:
            # no token file saved
            print('File not found')
            self.get_token()
        except ValueError:
            # token file corrupted
            print('File corrupt')
            self.get_token()

    def token(self):
        return self.__token

    def time(self):
        return self.__time


token = Token()

print(token.token()) # access your token

 

 

 

 

I don't believe this method allows you to grant scopes.  Really missing this capability that Spotify has removed

Really miss this part, managed to get a sort of "Get Token" button behavior with the following bash script:

CLIENT_ID="..."
CLIENT_SECRET=".."
SCOPES="user-read-currently-playing,user-modify-playback-state,user-read-playback-state"
curl -X "POST" -H "Authorization: Basic $(echo -n "$CLIENT_ID:$CLIENT_SECRET" | base64)" -d "grant_type=client_credentials&scope=$SCOPES" "https://accounts.spotify.com/api/token

But I have had issues with some endpoints such as me/player/next, I am getting the following response: (I am a premium user)

 

{
"error": {
"status": 403,
"message": "Player command failed: Premium required",
"reason": "PREMIUM_REQUIRED"
}
}

And it was kind of annoying to create an app for quick exploring/testing purposes.

 

PLEASE BRING BACK THE BUTTON DEAR SPOTIFY 😞

The removal of this button feels frustrating. In addition, the sample code provided in "Tutorials" on how to authorize seems to be incomplete - the node code required a few modifications to even run. Not a very beginner friendly Tutorial, especially when node is the only sample setup Spotify provides.

On some endpoints there is a similar functionality to the old "Get Token" - e.g. on https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks there is a "Try it" option that gets a token for you (and directly updates the "Sample output" to real output), but it doesnt show you the token, so this can only be used on the endpoints where the "Try it" button happens to be shown. (Idk if there is a logic to which pages have it, there might be)

Please bring back the old way this used to work or if you have to hide the test token from the developers that are trying to work with the api make the "Try it" functionality available on all pages of the documentation!

I'm a beginner. I think it is very difficult for me to get my access token. So dear Spotify, can you add the function again?

Maybe I think you could add some easer and more understandable tutorials.

Has anyone found a better solution to this? I've tried the python file that HopC posted and was able to get a token but unfortunately ran into an "insufficient scope" error. Also tried the bash script but no luck. I'm working on a project that uses a token but have similarly been having a lot of trouble with getting an access token with the new interface of the Spotify for Developers page and being able to access a token easily.

Suggested posts