I can't retrieve an access token from Spotify

2019-08-20 07:38发布

问题:

I can't get an access token for my Android app from Spotify because I can connect to the end point.

I need the access token of my Android app and I tried the following: 1) Spotify Android auth library. I can't find the spotify-auth-version.aar they talk about. 2) building manually the url. Here is an example:

https://accounts.spotify.com/authorize?
client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxx&
response_type=code&
redirect_uri=http%3A%2F%2Fmarcoalunno.com.spotify_test%2Fcallback&
scope=playlist-read-private&
state=34fFs29kd09

When I try to connect I get every possible kind of error: "Missing required parameter: client_id", "INVALID_CLIENT: Invalid redirect URI", "Page not found", and even other I don't remember. I suppose that the problem is with the redirect_uri. So, I added several different redirect address and tried all of them, but nothing.

Here are my addresses as I added them in my app: https://spotify_test.com/callback http://marcoalunno.com/callback http://localhost:8888/callback/ http://marcoalunno.com.spotify_test/callback

and their encoded versions: https%3A%2F%2Fspotify_test.com%2Fcallback%0A http%3A%2F%2Fmarcoalunno.com%2Fcallback%0A http%3A%2F%2Flocalhost%3A8888%2Fcallback%2F%0A http%3A%2F%2Fmarcoalunno.com.spotify_test%2Fcallback

I know that many people asked about "INVALID_CLIENT: Invalid redirect URI" on Stackoverflow, but I couldn't find a conclusive answer and I'm getting seriously frustrated with this.

回答1:

As far as I know the .aar file is an old way to integrate this library to your project and now you only need to add the following line to your build.gradle:

compile 'com.spotify.android:auth:1.1.0'

More information and required further steps can be found on the libraries GitHub page.

Do you do something with the redirect URI on your server? I just used soundtrack://callback for my soundtrack named app. I guess you can use almost whatever you like as long as this matches the ones in your Spotify developer dashboard and in your manifest file of your android app.

With the help of the Spotify Auth library it should be easier to authenticate compared to the manual way. If you want to pursue the manual way, I can take a look into the url building process and edit this answer accordingly.

The relevant files in the spotify auth sample project are:

  • build.gradle - where you add the auth library

  • spotify-strings.xml - where you declare the redirect scheme and host for the manifest file. In my case scheme = "soundtrack" host = "callback"

  • MainActivity.java - where you create the request and handle the response.