I'm struggling to change the code from PHP-Guzzle format that i get from postman to Laravel-Guzzle format.
So i want to use this PHP - Guzzle from Postman.
$client = new Client();
$headers = [
'Authorization' => 'OAuth realm="xxx",oauth_consumer_key="xxx",oauth_token="xxx",oauth_signature_method="HMAC-SHA256",oauth_timestamp="123",oauth_nonce="xxx",oauth_version="1.0",oauth_signature="xxx"',
'Cookie' => 'NS_ROUTING_VERSION=LAGGING'
];
$request = new Request('GET', 'https://domainname.com/record/1234', $headers);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
from above code. i need to convert to laravel format. and here is what i've done
use GuzzleHttp\Client;
$guzzle = new Client;
$header = [
'Authorization' => 'OAuth realm="xxx",oauth_consumer_key="xxx",oauth_token="xxx",oauth_signature_method="HMAC-SHA256",oauth_timestamp="123",oauth_nonce="xxx", oauth_version="1.0", oauth_signature="xxx"',
'Cookie' => 'NS_ROUTING_VERSION=LAGGING'
];
$response = $guzzle->request('GET', 'https://domainname.com/record/1234', $header);
dd($response->getBody());
but i got error
Client error: `GET https://domainname.com/record/1234` resulted in a `401 Unauthorized` response: {"type":"https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2","title":"Unauthorized","status":401,"o:error (truncated...)
i tried to solve it but cannot. please help how to make it work on laravel.
401 Unauthorized mean you are missing something like: token. That's mean your connection have been made but your credencials is't right. So nothing wrong with your code.
Related
For some reason, I'm getting a 400 Bad Request error when I'm sending my authentication to FedEx for my API.
Here is my code:
$payload = [
'grant_type' => 'client_credentials',
'client_id' => self::$apiKey,
'client_secret' => self::$apiSecret
];
$api = new App_ApiTransaction('POST', self::$authEndpoint, $payload, App_ApiTransaction::ENCODE_JSON);
$api->addHeader( 'Content-Type', 'application/x-www-form-urlencoded');
I tried using GET, but I was getting a 405 error. Also, this is a custom header function.
What could be causing the issue?
Thank you,
Kevin
Found the solution, here is what I had to do:
$payload = ('grant_type=client_credentials&client_id='.self::$apiKey.'&client_secret='.self::$apiSecret);
Basically changed the Payload to the correct format
I'm trying to make a token request using guzzle and receive an error "400 Bad Request` response: {"error":"invalid_client"}". I can make the same request with cURL and HTTP_Request2 with no problem.
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
session_start();
if(isset($_GET['code'])){
$code = $_GET['code'];
$encodeB64 = base64_encode('{clientID}:{clientSecret}');
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://identity.reckon.com/connect/token',[
['headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],['Authorization' => 'Basic '.$encodeB64]],
['body' => ['grant_type' => 'authorization_code'],['code' => $code],['redirect_uri' => '{redirectURI}']]
]);
$body = $response->getBody();
echo $body;
}
These are the details of how to make a token request with this API:
URL: https://identity.reckon.com/connect/token
Type: POST
Body: grant_type=authorization_code&code={code}&redirect_uri={redirect url}
Headers:
Content-Type = application/x-www-form-urlencoded
Authorization: Basic{client id:client secret encoded in base64}
Not sure where I'm going wrong.
I have worked it out. The answer was the following:
<?php
require 'C:/Users/Shane/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
session_start();
if(isset($_GET['code'])){
$code = $_GET['code'];
$encodeB64 = base64_encode('{client id}:{client secret}');
$authbody = 'grant_type=authorization_code&code='.$code.'&redirect_uri={redirect url}';
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://identity.reckon.com/connect/token',['headers' =>
['Content-Type' => 'application/x-www-form-urlencoded','Authorization' => 'Basic '.$encodeB64],
'body' => $authbody]);
$body = $response->getBody();
echo $body;
I have recently gone through {"error":"invalid_client"} with Guzzle, the error actually tells you specifically if something wrong with clientId or clientSecret. In my case I had first letter of clientSecret capitalized. It took a while to figure it out.
I am trying to consume a REST API (third party) and need to send a XML file along with request. I am trying to set file in the following way:
My Code:
use Zend\Http\Request;
use Zend\Http\Client;
use Zend\Stdlib\Parameters;
$request = new Request();
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'text/xml; charset=UTF-8'
));
$request->setUri("<third-party-api-url>");
$request->setMethod('POST');
$request->setFiles(new Parameters(array("/path/to/xml/file.xml")));
$client = new Client();
$client->setAuth("<username>", "<password>", \Zend\Http\Client::AUTH_BASIC);
$response = $client->dispatch($request);
print_r($response->getBody());
When executing the above code, I am getting following response:
[Error decoding xml body: org.xml.sax.SAXParseException; Premature end
of file.
I guess the XML is truncating to zero length file which is causing this error. Can anyone please help me to guide how to sent file in request object?
Thanks in advance
Dileep
Use the client instead:
$client = new Client();
$client->setRequest($request);
$client->setFileUpload("/path/to/xml/file.xml", 'file1');
$response = $client->send();
I'm trying to fetch orders data on my InfusionSoft account. I can do it using the command line but the Guzzle code gives me 401 Unathorized. I suppose I'm doing something wrong and not able to pass the params correctly. Can someone help?
Here's what works from the command line:
curl -G --data "access_token=abcdefgh12345678" https://api.infusionsoft.com/crm/rest/v1/orders?limit=1&offset=100&order_by=id
And here's the (supposedly) equivalent code from PHP:
$token = 'abcdefgh12345678';
$requestBody = array('access_token' => $token);
$url = 'https://api.infusionsoft.com/crm/rest/v1/orders?limit=1&offset=100&order_by=id';
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', $url, array(
'form_params' => $requestBody
));
$response = (string) $response->getBody();
You are sending a GET request, and a GET request cannot contain a body.
curl uses --data according to the request method, so for GET it adds the access token to the URL as a GET-parameter. So should you.
I have this curl request from the API docs
curl https://apidev.aaa.co.uk/api/v1/vouchers/2869512_1-9FDD --user TOKAN-HO:2eL4aKsSGCaN4FH8 -k
Now I'm trying to call this request with Laravel 5.1 using Guzzle library, so I do this
public function testacorne()
{
$client = new GuzzleHttp\Client();
$res = $client->get('https://apidev.aaa.co.uk/api/v1/vouchers/claimed', [
'form_params' => [
'user' => 'TOKAN-HO:2eL4aKsSGCaN4FH8'
]
]);
dd($res);
}
This is what i get
ClientException in RequestException.php line 107:
Client error: `GET https://apidev.aaa.co.uk/api/v1/vouchers/claimed` resulted in a `401 Unauthorized` response:
{
"Message": "Authorization has been denied for this request."
}
But the username and password are correct. How can I fix this issue?
Most likely --user in this case is used for Basic Auth with cURL. Try sending authorization data not by form data, but in the same way as cURL.
$client = new GuzzleHttp\Client();
$response = $client->get('https://apidev.aaa.co.uk/api/v1/vouchers/claimed', [
'auth' => [
'TOKAN-HO', '2eL4aKsSGCaN4FH8'
]
]);