Skip to content
Advertisement

Spotipy. Can`t pass the second part of oAuth (Python)

I want to create an app which will add some sorts of songs to the Spotify playlist. So it`s might be a good idea to use Spotipy (Python Library) to solve it.

To add song to the playlist I need to know its URI. To know its URI I should use method called search (https://spotipy.readthedocs.io/en/2.19.0/#spotipy.client.Spotify.search). It requires some inputs and one of them is Authorization, which requires access token (link for docs https://developer.spotify.com/documentation/web-api/reference/#category-search). Then I used Authorization Guide from Spotify to authorize(link for AuthGuide https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow) and I stacked.

The first step is pretty easy:

self.query = {
            'client_id': client_id,

            'response_type': 'code',
            'redirect_uri': 'andriime.github.com/pc/'
        }


        self.info_code = requests.get(url=SPOTIFY_URL, params=self.query)
        self.authorization_code = self.info_code.text

But second step is really scary. I search for its solution over whole Internet and there isnt any one normal guide HOW to pass it. But this is the best I`ve found:

Spotify API {‘error’: ‘invalid_client’} Authorization Code Flow [400]

Please, if you know how to deal with it or there is an answer, which I missed just write here!

EDIT

If I print self.info_code response code is 200. client_id and client_secret are environmental variables and I defined them above (I didn`t post this code). My step-2-code looks like this:

self.authorization_code = self.info_code.text

        code_payload = {
            'grant_type': 'authorization_code',
            'code': self.authorization_code,
            'redirect_uri': redirect_uri,
        }


        auth_str = '{}:{}'.format(client_id, client_secret)
        b64_auth_str = base64.urlsafe_b64encode(auth_str.encode()).decode()
        print(b64_auth_str)

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic {}'.format(b64_auth_str)
        }

        post_request = requests.post(url=API_URL, data=code_payload, headers=headers)
        print(post_request.text)

The response text is {“error”:”invalid_grant”,”error_description”:”Invalid authorization code”}. There is an answer on StackOverflow, but it doesn`t work for me (How to fix ‘error: invalid_grant Invalid authorization code’ when asking for reshresh_token from Spotify API?), because I didnt make that mistake which is written about there.

Also there is an answer on Spotify community forum, but there is no answer there. There is one question on another forum,but it`s non-answered too

Links:

1.https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-get-access-token-using-authorization-code-flow/m-p/5098258

2.https://developer.salesforce.com/forums/?id=906F00000009B2AIAU)

So again ask you to answer if you know how it should work. Many thanks!

Advertisement

Answer

Okay, I find out how to do it. This video really helped me (https://www.youtube.com/watch?v=yAXoOolPvjU), however it`s about javascript, but the consept is the same.

After the first step (after getting self.info_code response) you should check some things:

  1. Is your redirect uri is the same as in DashBoard and was it written there in right format (https://example.com/callback/), where I made a mistake – I had ‘redirect_uri parametr in self.query as ‘andriime.github.com/pc/’ not ‘https://andriime.github.io/pc/’ (its also important to remember that there isn`t any github.com, only github.io, so right link correctly right into DashBoard)

2.”response_type” in your params (requests input) should equal to ‘code’

  1. After this, you should get a link of your request (I didn`t find how to do it beatifully, so just wrote this:

print(f'{SPOTIFY_URL}?client_id={client_id}&client_secret={client_secret}&response_type={self.query["response_type"]}&redirect_uri={"https://andriime.github.io/pc/"}')

where SPOTIFY_URL equals to “https://accounts.spotify.com/authorize”

then you just copy and paste link into the browser and click some “accept” buttons. After this, you`ll be redirected to the page you pasted as ‘redirect_uri’ and then you should click on the link in browser and you should see “code” param there after your “redirect_uri”.

I know, that this part is a bit complecated, but you should watch the video I linked above, it would be useful and easier to understand

Then you can just copy the second part of my code and use it. But, of course, change the params.

        code_payload = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': redirect_uri,
        }


        auth_str = '{}:{}'.format(client_id, client_secret)
        b64_auth_str = base64.urlsafe_b64encode(auth_str.encode()).decode()

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization': 'Basic {}'.format(b64_auth_str)
        }
        new_response = requests.post(url=API_URL, headers=headers, params=code_payload)
        print(new_response.text)

This answer also can be much impoved, so I really want to see your feedbacks or another answers here to make this method easier and more beatiful

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement