Guzzle HTTP Client and Shopify access token exception - php

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

Related

Can’t get a refreshed access_token using spotify api and laravel

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
]
);

Laravel EBay API Authorization Code Request Gives Error, Unsupported Grant Type

$client = new Client();
$response = $client->request(
'POST', /*instead of POST, you can use GET, PUT, DELETE, etc*/
'https://api.sandbox.ebay.com/identity/v1/oauth2/token',
[
'headers' => [
'Content-Type'=>'application/x-www-form-urlencode',
'Authorization'=>'Basic '.base64_encode('<'.env('EBAY_CLENT_APP_ID').'>:<'.env('EBAY_CLENT_APP_SECRET').'>'),
],
'form_params' => [
'grant_type' => 'authorization_code',
'code' => $req->code,
'redirect_uri' => env('EBAY_REDIRECT_URI')
]
]
);
$output = json_decode($response->getBody());
dd($output);
I am using guzzlehttp 7.4 and laravel 8.65. All the credentials I am using is all sandbox so I am not sure what I am doing wrong. I keep getting the same error unsupported_grant_type etc.

How to convert cURL to Guzzle GET

First of all: First question here in Stack Overflow, quite an honor! :D
How do i convert the following curl into Guzzle?
curl -u USER_KEY:USER_SECRET https://api.apiexample.com/example/oauth/oauth?grant_type=client_credentials
I tried the following, but i kept getting 403 - Forbidden error:
$res = $client->request('GET', 'https://api.apiexample.com/example/oauth/oauth',[
'auth' => ['USER_KEY', 'USER_SECRET'],
'header' => [
'Content-Type' => 'application/x-www-form-urlencoded'
],
'form_params' => [
'grant_type' => 'client_credentials'
]
]);
What could be wrong with my code?
Edit: Forgot one thing: This request returns a token that lasts for 30 minutes. Any tip to run this function only when the token is about to expire?
I would recommend maybe reformatting your request by putting your constant values in the Client object declaration and also wrapping the request in a try-catch block. The try-catch block provides the added benefit that it will help you debug the response. Feel free to alter the catch statement as you wish.
$client = new Client([
'base_uri' => 'https://api.apiexample.com/example/',
'auth' => [
'USER_KEY',
'USER_SECRET'
]
]);
try {
$response = $client->request( 'GET', 'oauth/oauth', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => [
'grant_type' => 'client_credentials'
]
]);
} catch (Exception $e) {
error_log($e->getCode());
error_log($e->getMessage());
error_log(print_r($e->getTrace(), true));
}

Guzzle in laravel keeps loading, not returning values

Im using Laravel passport for API authentication. I have two routes
/api/login
and
/oauth/token
Since I cannot hardcode my client id and the login receives from JS and the params and client id is hardcoded inside a login method(laravel), Im trying to post the values using Guzzle (6.0) to oauth/token (POST requests).
I followed a youtube video and there it works but not mine. Iam using 5.6, not sure which version was in the video. Could someone help?
Below is the Video
https://www.youtube.com/watch?v=HGh0cKEVXPI&t=838s
Below is the code
$http = new GuzzleHttp\Client();
$request = $http->post(URI, '/oauth/token', [
'form_params' => [
'username' => 'bar',
'password' => 'xxxxxx',
'client_id' => 2,
'grant_type' => 'password',
'client_secret' => '00000000000000000'
]
]);
return $request;
You are not getting the response only returning guzzle $request initalization so add getBody()
$http = new GuzzleHttp\Client();
$request = $http->post(URI, '/oauth/token', [
'form_params' => [
'username' => 'bar',
'password' => 'xxxxxx',
'client_id' => 2,
'grant_type' => 'password',
'client_secret' => '00000000000000000'
]
]);
return $request->getBody();
I think you are trying to request in build-in server.
So You try two servers to send the request.
It will be working.
Like localhost:8000 and localhost:9000 server
Use this command
php artisan serve
php artisan serve --port=9000
Thanks.
You should check the status first to make sure that everything is okay by using
$request->getStatusCode();
You can get your response by
$request->getBody();
you can see also full documentation of using GuzzulHttp from Here
http://docs.guzzlephp.org/en/stable/

How to use LinkedIn People Search API

As there are several changes in LinkedIn People Search API as of now,please explain how to use this API for people search by company and etc...
I have read
https://developer-programs.linkedin.com/documents/people-search-api
and other document but not getting connection with search link and API key and all.As explained in document we can search some thing like this
https://api.linkedin.com/v1/people-search?keywords=Princess
but where we need to put API KEY and all. I am new to APIs so please if possible explain this.
I have also go through other blogs but they are old and not applicable.
If possible please also mention possibility and things we cann't do.
LinkedIn's People Search API has not been available to the open developer community since May, 2015.
You can apply to be a developer partner at: https://developer.linkedin.com/partner-programs/apply
If you are an official partner of LinkedIn and still have access to that API, you should follow up with your assigned Partner Engineering representative for further assistance, rather than public forums.
Use Postman to follow this tutorial:
https://developer.linkedin.com/docs/oauth2
On succes you can click to get the PHP code you need to create the request with OATH2. I have no developer account on LinkedIn so that part you have to do yourself.
The example GET request would look like this in PHP:
Step 2
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://www.linkedin.com/uas/oauth2/authorization');
$request->setRequestMethod('GET');
$request->setQuery(new http\QueryString(array(
'response_type' => 'code',
'client_id' => '123456789',
'redirect_uri' => 'https://www.example.com/auth/linkedin',
'state' => '987654321',
'scope' => 'r_basicprofile'
)));
$request->setHeaders(array(
'cache-control' => 'no-cache'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
Step 3
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://www.linkedin.com/uas/oauth2/accessToken');
$request->setRequestMethod('POST');
$request->setQuery(new http\QueryString(array(
'grant_type' => 'authorization_code',
'code' => '987654321',
'redirect_uri' => 'https://www.myapp.com/auth/linkedin',
'client_id' => '123456789',
'client_secret' => 'shhdonottell'
)));
$request->setHeaders(array(
'postman-token' => 'bee6f5d7-a0e6-4a76-6ef8-930c95af53a6',
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded',
'host' => 'www.linkedin.com'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

Categories