0
votes

I am currently implementing a feature that you have the ability to save a song displayed on my iOS application (written with Swift) and this save button allows the song to be appended to the user's Spotify library. According to the Spotify Developer guide, the only scope required for this feature is user-library-modify when authorizing the app with the user. The url to be opened goes like this:

https://accounts.spotify.com/authorize/?client_id=my_client_id&response_type=code&scope=user-library-modify&redirect_uri=http://my_redirect_uri

This all works perfectly - the url is opened for the user to approve of the changes my app can make and the callback url with the required code is in the url is opened.

The next step in performing the required function is to get an exact token in order to use the api, which is done by calling the url:

https://accounts.spotify.com/api/token?grant_type=client_credentials&client_id=my_client_id&client_secret=my_client_secret&response_type=code&redirect_uri=http://my_redirect_uri&code=the_code_I_just_retrieved

With this url, a json file is returned with the new token and info with it, BUT when looking at the permitted scopes the token has, it is empty:

["scope": , "token_type": Bearer, "access_token": the_token_string, "expires_in": 3600]

Also, when I still try to perform the request it returns:

["error": { message = "Insufficient client scope"; status = 403; }]

In this lengthy process, what am I doing wrong? In case you are wondering, here are a few things I have tried without success:

1) Re-listing the scopes in the explicit token request

2)Adding utf-8 encoding to the redirect uri (not sure if this changes anything)

3)Adding many other scopes (although this clearly does not target the problem)

If anyone knows what I am doing wrong or has any suggestions as to what I should try, I am grateful for any helpful response!

1

1 Answers

2
votes

I have found my mistake. The grant_type I have entered in my url set to client_credentials. However, this method of accessing the web API only permits the usage of publicly available data, not user info. Therefore, this method of authorization does not accept the parameter scope, forcing the spotify account service to ingnore this additional parameter. The other options DO allow accessing the user data, which are:

authorization_code, and refresh_token

The way this now has to be done is to:

1) Authorize the user regularly (with supplying the scopes) to retrieve the initial authorization code

2) Then, with this code, make the token request, but specifying the grant_type to be set as authorization_code

3) You have then received a valid access_token valid for one hour AND a refresh_token

4) Use the access_token when necessary and when this token expires, make another token request, but this time with the grant_type set as refresh_token and setting the code parameter to the previously gained refresh_token

5) You now have the next access_token and refresh_token

6) Repeat steps 4-5 until infinity