Laravel Guzzle params - php

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

Related

How to change PHP Guzzle from postman to Laravel Guzzle format

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.

converting a $_GET cURL request to Laravel 8.x http request not working as expected (returns 403 error)

I have the following cURL request in Postman which works perfectly fine, however I am having issues getting the same response data once I have converted this cURL request into a Laravel 8.x format using the HTTP facade.
curl request
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://proclubs.ea.com/api/fifa/clubs/info?platform=ps4&clubIds=1741008');
$request->setRequestMethod('GET');
$request->setOptions(array());
$request->setHeaders(array(
'Referer' => 'https://www.ea.com/'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
Laravel HTTP request
// have added use Illuminate\Support\Facades\Http; at the top of class
$url = 'https://proclubs.ea.com/api/fifa/clubs/info';
$params = [
'platform' => 'ps4',
'clubIds' => 1741008
];
$response = Http::withHeaders([
'Referrer' => 'https://www.ea.com/',
])->get($url, $params)->json();
dd($response);
expected output
{
"1741008": {
"name": "BanterburyFC",
"clubId": 1741008,
"regionId": 4344147,
"teamId": 112092,
"customKit": {
"stadName": "Wanda Metropolitano",
"kitId": "1836515329",
"isCustomTeam": "0",
"customKitId": "7623",
"customAwayKitId": "7623",
"customKeeperKitId": "5012",
"kitColor1": "1987272",
"kitColor2": "0",
"kitColor3": "16777215",
"kitAColor1": "16734520",
"kitAColor2": "0",
"kitAColor3": "16777215",
"dCustomKit": "0",
"crestColor": "1987272",
"crestAssetId": "99040402"
}
}
}
actual output
null // getting a 403 error response
When running the debug() command in Laravel I can see the following error
old SSL session ID is stale, removing * Mark bundle as not supporting multiuse < HTTP/1.1 403 Forbidden - how do I fix this?
You have misspelled the header name. It is Referer with one r not two. Change your code to this and it should work:
$response = Http::withHeaders([
'Referer' => 'https://www.ea.com/',
])->get($url, $params)->json();

GAE PHP 7 403 FORBIDDEN error while got URL to upload file

I got some problem while managing migration from PHP5 to PHP7.
Actually, what I was doing with php5, to get an URL where upload a file, was something like this:
$options = [ 'gs_bucket_name' => 'mybucket.appspot.com' ];
$myvar = $_GET['myvar'];
$upload_url = CloudStorageTools::createUploadUrl($myvar, $options);
return $upload_url;
I've uploaded the library and now I'm trying to use
Google\Cloud\Storage\StorageClient
I've converted the code above to something like this:
$bucketName = 'mybucket.appspot.com';
$objectName = $_GET['myvar'];
$storage = new StorageClient([
'scopes' => [
'https://www.googleapis.com/auth/iam',
'https://www.googleapis.com/auth/devstorage.full_control',
]
]);
$bucket = $storage->bucket($bucketName);
$object = $bucket->object($objectName);
$upload_url = $object->signedUrl(
new \DateTime('90 min'),
[
'version' => 'v4',
]
);
return $upload_url;
I got a 403 FORBIDDEN error
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/myproject#appspot.gserviceaccount.com:signBlob?alt=json` resulted in a `403 Forbidden` response:
{
"error": {
"code": 403,
"message": "IAM Service Account Credentials API has not been used in project 123456 (truncated...)
....
Some suggestions? In the PHP5 version upload is working (with the older code), so I suppose my app angine service account has the correct permissions setted.
Thanks
Solved by passing .json file for authentication.
$storage = new StorageClient([
'projectId' => "my_project",
'keyFilePath' => 'key/serviceaccount.json',
]);

ClientException: Client Error giving '401 Unauthorized' error in GuzzleHttp using laravel

I'm trying to use GuzzelHttp for send HTTP request, but when I run it I get the error:
Client error: POST http://localhost:8100/api/customers?company_id=9 resulted in a 401 Unauthorized response: {"message":"Unauthenticated."}
Below is the code I wrote:
Route::get('/testguzzel', function() {
$client = new \GuzzleHttp\Client();
$res = $client->request('POST',
'http://localhost:8100/api/customers?company_id=9',
['auth' => ['username', 'password'],
'headers' => ['Accept'=> 'application/vnd.twareeq.v1+json']]);
return $res->getBody()->getContents();
});
How to fix this problem?

Microsoft Graph SDK for PHP - 400 bad request while trying to get data

I have tried a lot to find out my issue but no luck there.
I am using Guzzle for my authentication and it's giving me the token. So there is no issue. The problem is while i am trying to get data it is showing me
" An uncaught Exception was encountered
Type: GuzzleHttp\Exception\ClientException
Message: Client error: GET https://graph.microsoft.com/v1.0/me resulted in a 400 Bad Request response: { "error": { "code": "BadRequest", "message": "Current authenticated context is not valid for this request. (truncated...) "
Here is my code below
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $appId,
'client_secret' => $appSecret,
'resource' => 'https://graph.microsoft.com/',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$graph = new Graph();
$graph->setAccessToken($accessToken);
$user = $graph->createRequest("GET", "/me")
->setReturnType(Model\User::class)
->execute();
echo "Hello, I am $user->getGivenName() ";
Note: I have successfully got the token, and i am using CodeIgniter framework where CSRF is false. Also i have given the permission for the app User.Read.All, User.ReadWrite.All
Please help me out to solve of this problem.
Thanks in advance.
You cannot call /me using Client Credentials. The purpose of the Client Credentials is to authenticate an application without a user. Therefore there is no way for the API to translate which user you mean when you call /me.

Categories