0
votes

I'm trying to communicate with Spotify using web API. So here's the case. In order to retrieve any information from Spotify, first I need to be authorized by Spotify. To do so, I need to send a post request to https://accounts.spotify.com/api/token containing authorization credentials(containing client_id and client_secret), and then in response, I should receive an access token which later I shall use to retrieve any other information. Here's a quick Spotify documentation on how it works :

enter image description here

So the problem here is that I'm doing everything I should but I don't get the token in response.

Here's my code using the Guzzle package in laravel :

enter image description here

And instead of an access token as a response, This HTML output is what I'm receiving from Spotify in return (Without any further information of the problem and any error code):

enter image description here

1

1 Answers

1
votes

It is possible that you are misinterpreting this line?

Authorization: Basic <base64 encoded client_id:client_secret>

It looks like you are doing this:

base64_encode($client_id) . ":" . base64_encode($client_secret)

But perhaps they want this:

base64_encode($client_id . ":" . $client_secret);

That is, assuming you have base 64 encoded them at all as this is not actually shown in your code.


Additionally, the documentation states that it wants application/x-www-form-urlencoded encoding.

# Sending Form URL Encoded Requests
https://laravel.com/docs/8.x/http-client#sending-form-url-encoded-requests

To meet this requirement, it looks like you may need to add asForm() to the request.

$response = Http::withHeaders(...)->asForm()->post(...);