Spotify API token refresh

I cannot figure out how to configure Spotify’s APIs to work for any amount of time. The tokens always die :skull_and_crossbones: and I cannot figure out how to refresh them. I want to be able to search Spotify podcasts from our app.

I’ve seen forum topic, “Spotify Refresh Token” and I don’t understand how it relates to what I’ve done. Here’s my configuration:

All help is greatly appreciated!

Do you mean it die in API connector? Or when you use the app?

The token times out and eventually becomes invalid

…so both

Because this is normal in API Connector. Bubble doesn’t refresh the API connector. But for your user, if this is the case, do you use differents Social network login? Ex: A user can create an account using Google and you try to authenticate this user to Spotify too?

In my case, the user is my app. What can i do differently to make it work?

You mean that you don’t need to authenticate on behalf on another user, but just on your own spotify account?

Yes, that’s correct.

In this case, you don’t need to use authorization code WF but client credentials WF

oAuth2 custom token in Bubble

Sorry, just checked and I think it will not work. Client credentials are fot that normally, but Spotify doesn’t allow access to user reccources.

You will need to create your own process becauseBubble will only set the user agent oAuth2 for the current authenticated agent. If you want to use theses credentials while another user is logged in, it won’t work.

When successfully logged into Spotify you’ll be granted with an Access Token (long code starting with “B”) that will expire in 3600 seconds and a Refreshable Token (starting with “A”) that you must store securely in your database and “refresh” (if expired) every time you communicate with their API.
You can find some examples in their API documentation.

Thanks! I have hope again! :slight_smile:
I still don’t know how this translates into configuring Bubble’s API Connector to make this work. Can I use Authentication: OAuth2 User-Agent Flow as I have above? If so, what do I need to configure differently? If not, which authentication type do I choose and how would that be configured?

Hi! Did you ever get a solution to this? Trying to figure out refresh tokens with Spotify’s API and Bubble now… thank you!!!

I don’t have a solution yet. I wasn’t able to figure it out on my own.

watch this video
do not use api connector oauth 2 workflow but make your own workflow for getting tokens and save to user.

if its only 1 spotify account you need then not each user needs own token but just create 1 token for entire app every eg 1 hour.

Hey @TipLister , for my app like you mentioned, I will only need my token since the user will just be accessing public information from the Spotify Search API, and I’m still not sure where and what to call to to request a refresh token every hour. heres my setup below, I am just plugging in the new bearer code every hour that I request manually from the Spotify developer console. I watched that video and I’m not sure how to translate it to Spotify.

My end point for my calls is just https://api.spotify.com/v1/search
Im not aware of a url of Spotify’s that I can call to to request a token like google’s “google.apis.com/token

How would you set up a work flow for this?

https://benwiz.com/blog/create-spotify-refresh-token/ - this post kind of explains it but i think it is for having Spotify Users log in which i dont need.

Thanks for all your help!

also this is the url im at when requesting a new token

https://accounts.spotify.com/en/authorize?response_type=token&redirect_uri=myredirecturl&client_id=12345&state=xqsr8h

hi zachary, there are 2 videos by airdev on this, probably easiest.
https://www.youtube.com/results?search_query=bubble+spotify+api

Thank you @TipLister ! I worked through those videos and got everything working.

I know this is a long shot but my goal is for the user to not have to log into Spotify, but to piggyback off my authorization/account. I was wondering if you think it is possible for the backend of bubble to automatically refresh my token without each user having to do anything at all, since no user data is needed for my app, just search results, which can be accessed through my one account. Would there be a way for it to automatically log me in and do the requests i.e. “Do every 3600 seconds” workflow behind the scenes? Or is it unavoidable because one of the steps in the process requires someone to go to an external address.

Otherwise I will be up every hour to refresh the authorization manually lol. Thanks in advance for any tips that you may have!

so add a new datatype called token with the fields until when it expires [a date], the access and the refresh token.
in this add just one row manually.

then on page load check if we are past expiry date
if yes schedule an api workflow
this api workflow is in backend and uses refresh token to create new access token.

as it is on page load it works for every user.

[if you had 1000 users you would have 1000 rows but here 1 is fine]

1 Like