First time using HTTP Client and Spotify API in Laravel.
The first step is to get a code by visiting
https://accounts.spotify.com/authorize?client_id=c1990...deed3&response_type=code&redirect_uri=http%3A%2F%2Fexample.test%2F&scope=user-read-currently-playing%20user-top-read
I then copy the code from the url after being redirected.
Then using curl -
curl -H "Authorization: Basic YzE5OT...Q2ZjA=" -d grant_type=authorization_code -d code=AQBX...X5zg -d redirect_uri=http%3A%2F%2Fexample.test%2F https://accounts.spotify.com/api/token
This returns the refresh token in JSON format -
{
"access_token":"BQBQL...vNDQ",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":"AQCy...areM",
"scope":"user-read-currently-playing user-top-read"
}
But then I can't seem to get an access token using the refresh_token.
I'm getting "Bad Request" statusCode: 400 in my app
$response = Http::withHeaders([
'Authorization' => `Basic YzE5O...Q2ZjA`,
'Content-Type' => 'application/x-www-form-urlencoded'
])
->asForm()
->post(
'https://accounts.spotify.com/api/token',
[
'grant_type' => 'refresh_token',
'refresh_token' => 'AQC7...YphY'
]
);
Here is the documentation https://developer.spotify.com/documentation/general/guides/authorization/code-flow/.
Has anyone implemented this before in Laravel and if so how?
I have no idea about the Spotify API, but I am 99% sure your error is ->asForm(), you are sending that as a form instead of a normal request... so your code may need to be like this:
$response = Http::withToken('YzE5O...Q2ZjA')
->post(
'https://accounts.spotify.com/api/token',
[
'grant_type' => 'refresh_token',
'refresh_token' => 'AQC7...YphY'
]
);
See that I have removed ->asForm() and Content-Type (not sure why you are using that, it is a normal API... and ->asForm() already sets the same content you have manually set...
This is the Spotify API and I do not see any need to set the Conetnt-Type.
My bad, you need to set the ->asForm(), so the code should be:
$response = Http::withToken('YzE5O...Q2ZjA')
->asForm()
->post(
'https://accounts.spotify.com/api/token',
[
'grant_type' => 'refresh_token',
'refresh_token' => 'AQC7...YphY'
]
);
But I still think you are missing something. Check that your refresh_token is correct. Also lookf for more debugging output
This worked for me.
'Content-Type' => 'application/x-www-form-urlencoded' was removed from withHeaders
$response = Http::withHeaders([
'Authorization' => 'Basic ' . 123...123,
])
->asForm()
->post(
'https://accounts.spotify.com/api/token',
[
'grant_type' => 'refresh_token',
'refresh_token' => 123...456
]
);
Related
I'm getting Client error: POST https://testing-shop.myshopify.com/admin/oauth/access_token resulted in a 400 Bad Requestwhile trying to exchange temporary code for access token in Shopify. I'm using the latest version of Guzzle HTTP client and in Chrome, Windows 8.1. What's even more weird is that it worked before.
$client = new Client();
try{
$response = $client->request(
'POST',
"https://{$store}/admin/oauth/access_token",
[
'form_params' => [
'client_id' => $api_key,
'client_secret' => $secret_key,
'code' => $query['code']
]
]
);
}catch(Exception $e){
var_dump($e);
}
I also checked all my variables ($api_key, $secret_key)... and they're good which means they have values. What could be the problem here that I missed? TIA
EDIT:
It turned out to be the problem when registering the web hook .
$response = $client->request(
'POST',
"https://{$store}/admin/webhooks.json",
[
'webhook' => [
'topic' => 'app/uninstalled',
'address' => 'http://example.com/shopify/uninstall',
'format' => 'json'
]
]
);
This code causes the error but I'm not though why.
Think I've got it. And it fits with having worked then stopped working:
After July 1st 2018, apps will be required to use HTTPS webhook addresses.
See: https://help.shopify.com/en/api/getting-started/webhooks
I'm trying to delete files from my CloudFlare cache using PHP. Using Guzzle I've done this:
$client = new \GuzzleHttp\Client;
$response = $client->delete('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'query' => [
'files' => 'https://example.com/styles.css,
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);
But when I run this I get an error:
Client error: DELETE https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache?files=https%3A%2F%2Fexample.com/etc resulted in a 400 Bad Request response: {"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\", \"files\", \"tags\" (truncated...)
I can't get it to work using Postman either. I put in the required headers and try to set a key of files or files[] with the URL but it doesn't work. I've also tried data with raw JSON as the value like {"files":["url"]} (along with a JSON content-type header) but get the same error. It thinks I'm not sending the files key.
The method for purge_cache is POST instead of DELETE (Source: https://api.cloudflare.com/#zone-purge-files-by-url).
The payload is not sent as 'query', but as 'json'.
Files should be an array, not a string.
So the correct syntax should be....
$client = new \GuzzleHttp\Client;
$response = $client->post('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'json' => [
'files' => ['https://example.com/styles.css'],
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);
I'm trying to figure out a way to make a request to a REST api using some PHP client.
Authorization: Token token="CREDENTIALS"
I can successfully curl it by using
$ curl -H 'Authorization: Token token="CREDENTIALS" https://uriexample.com
But I can't figure out a way to set this header in any PHP client I tried (Guzzle and Httpful).
Would anyone know how can I do this with ANY PHP client? I just don't wanna code this client from scratch :(
The Guzzle docs have loads of examples if you dig into them a little.
http://docs.guzzlephp.org/en/latest/quickstart.html#making-a-request
http://docs.guzzlephp.org/en/latest/request-options.html#headers
<?php
// Create HTTP client with headers for all requests
$client = new GuzzleHttp\Client([
'base_uri' => 'https://uriexample.com',
'headers' => [
'Authorization' => 'Token token="CREDENTIALS"',
],
]);
// Dispatch GET request
$client->request('GET', '/');
// OR
// Create HTTP client
$client = new GuzzleHttp\Client([
'base_uri' => 'https://uriexample.com',
]);
// Dispatch GET request with specific headers
$client->request('GET', '/', [
'headers' => [
'Authorization' => 'Token token="CREDENTIALS"',
],
]);
In my Laravel application, I periodically need to POST data to an API using Guzzle.
The API users a bearer token to authenticate, and requests and accepts raw json. To test, I accessed the API using Postman, and everything worked wonderfully.
Postman Headers:
Accept:application/json
Authorization:Bearer [token]
Content-Type:application/json
And Postman Body:
{
"request1" : "123456789",
"request2" : "2468",
"request3" : "987654321",
"name" : "John Doe"
}
Postman returns a 200, and a JSON object as a response.
Now, when I try the same with Guzzle, I get a 200 status code, but no JSON object gets returned. Here's my Guzzle implementation:
public function getClient($token)
{
return new Client([
'base_uri' => env('API_HOST'),
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
]);
}
$post = $client->request('POST', '/path/to/api', [
'json' => [
'request1' => 123456789,
'request2' => 2468,
'request3' => 987654321,
'name' => 'John Doe',
]
]);
Is there some trick to POSTing JSON with Guzzle? If not, is there a way to debug what's going on under the hood?
I cannot, for the life of me, understand what the difference is between the Postman POST and the Guzzle POST.
You have to use headers config sections for headers, not the root level.
return new Client([
'base_uri' => env('API_HOST'),
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
],
]);
I have a problem with translating curl to guzzle request.
In docs to create a user i just need to post:
$ curl -XPOST -d '{"username":"test", "password":"super_secret_password"}' -H "Content-Type:application/json" -u "$CLOUDMQTT_USER:$CLOUDMQTT_PASSWORD" https://api.cloudmqtt.com/user
In my project I cannot use curl, so i use guzzle:
$client = new Client();
$res = $client->post('https://api.cloudmqtt.com/user', ['auth' => ['xxx', 'xxx'], 'body' => ["username"=>"user", "password"=>"super_secret_password"]]);
And user is created, I can see new user on the users list on panel, but server is responsing with 500 when creating the user. What am I doing wrong? Maybe my guzzle request is wrong format? I have no idea
https://www.cloudmqtt.com/docs-api.html link to API
This will match up your Guzzle request to the curl request, although I can't say for sure that will solve your 500 error:
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $client->post('https://api.cloudmqtt.com/user',
[
'auth' => ['xxx', 'xxx'],
'body' => json_encode(
[
"username"=>"user",
"password"=>"super_secret_password"
]
)
]
);
The differences here include setting the Content-Type header and also encoding the body to json instead of an array (which may not have an effect here?).
EDIT:
It looks like the json parameter will automatically set the header and json_encode the body for you:
$client = new Client();
$response = $client->post('https://api.cloudmqtt.com/user',
[
'auth' => ['xxx', 'xxx'],
'json' =>
[
"username"=>"user",
"password"=>"super_secret_password"
]
]
);
Docs