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:
2.https://developer.salesforce.com/forums/?id=906F00000009B2AIAU)
So again ask you to answer if you know how it should work. Many thanks!