Quickblox API errors when using Guzzle 6 - php

I'm using "guzzlehttp/guzzle": "^6.1" and Laravel 4.2
I'm trying to make a post call to quickblox but return me 422 error. This is my code in the controller:
$client = new GuzzleHttp\Client();
$sessionGuzzle = $client->request('POST', 'https://api.quickblox.com/session.json',['headers' => ['Content-Type' => 'application/json' , 'QuickBlox-REST-API-Version'=>'0.1.0'],'json' => $jsonBody]);
$response = $client->send($sessionGuzzle);
dd($response->json());
GuzzleHttp \ Exception \ ClientException (422)
Client error: 422

The 422 is the HTTP status code - Unprocessable Entity.
Looks like the quickblox API is rejecting your call because your either sending the wrong headers or body.
Try changing 'json' => $jsonBody to 'body' => $jsonBody and make sure $jsonBody is a JSON encoded string.

According to the Quickblox documentation on errors your 422 error occurs when
User with login that has already been taken
According to the Quickblox documentation on authentication the timestamp that is provided must be +/- 10 minutes of NTP.

Related

POST with guzzle returns 417

I created an API to post JSON data to another API, but for some JSON it returns 417 error code.
I checked these JSONs and tried to re-post them, but i got the 417 again, the post is only working if i delete some values from the JSON.
The JSONs is always valid and it's around 1.5KB of data, but i cant figure it out why it's happening.
Guzzle version: 7.0, PHP version: 7.4
$guzzle = new \GuzzleHttp\Client([
'verify' => false,
'expect' => false
]);
$request = null;
$request = $guzzle->post(
$request_url,
[
'auth' => [self::$API_USERNAME, self::$API_PASSWORD],
'json' => [$request_body]
]
);
I've created a postman request and send the invalid JSON to the API direct without the middleware, now i can see the full error related to the invalid email address format.

"Empty Payload. JSON content expected" error calling Microsoft Graph (Guzzle & PHP)

I am trying to call the Microsoft Graph API to reset a passcode on a device registered with Intune. Unfortunately when I go to make the call, I receive an error stating that the JSON Payload is empty. The specific endpoint does not require a JSON payload, in fact it says to not include a body at all.
I attempted to add some JSON to see if that would satisfy the error, and I still receive the same error message.
Here is the call I am making:
$client = new Client();
try{
$client->post('https://graph.microsoft.com/beta/managedDevices/12345resetPasscode', [
'headers' => [
'Authorization' => 'Bearer 12345',
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'json' => json_encode(['hello' => 'world']),
]
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
dd($e->getResponse()->getBody()->getContents());
}
Here is the error I am receiving:
"Bad Request: Empty Payload. JSON content expected."
https://i.stack.imgur.com/gwwtJ.png
Here is the API Documentation I am working off: https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/intune_devicefe_manageddevice_resetpasscode
Using PHP 7 & Guzzle 6
Any help is appreciated!
I'm an engineer on the Microsoft Intune team, working on the integration between Microsoft Graph and Intune.
It looks like there is an error in the documentation (I will make sure that is fixed). The correct URL You should be using is:
https://graph.microsoft.com/beta/managedDevices/12345/resetPasscode
Where 12345 is a the id of the device.
Hope that resolves your issue
Peter

Getting "Invalid user credentials supplied" when sending request with Guzzle PHP

I am sending the absolutely same request to an endpoint with Guzzle PHP and Postman extension for Chrome.
When sending the request with Postman - I do recieve the response, but when I am sending the same request with Guzzle I get "Invalid user credentials supplied".
I have the Auth type Basic and supply the same username/password for both apps. Here is the code that I use for guzzle:
$credentials = [$client->api_username, $client->decrypted_password, 'basic'];
$result = $this->client->get($fullUrl, [
'auth' => $credentials
]);
I have dumped the credentials - it is the correct array. I have double checked the Guzzle docs. Funny thing is that when I am trying to send request for another user for the same endpoint - I do recieve the correct response, which made me think, that I might have typos in credentials - but I have rechecked - and even copy pasted from Postman - still cant recieve the response :/
Would you love to share what your variable $credentials has as data?
In case try the code below it was tested and works:
$client = new Client();
$this->results = $client->request('GET/POST', $uri, [
'debug' => true,
'query' => $arguments,
'auth' => [$username, $password],
'verify' => false
])->getBody();
And if you ask about the other arguments such as verify, this property tells guzzle to disable certificate verification.
Read below: http://docs.guzzlephp.org/en/stable/request-options.html#verify
For more information about auth: http://docs.guzzlephp.org/en/stable/request-options.html#auth
Could you compare the Authentication header from Postman and from Guzzle? If you get an exception from Guzzle, you can get it like $exception->getRequest()->getHeader('Authentication').

Using Oauth with GuzzleHttp and Magento API in PHP

I'm trying to access Magento's REST API in PHP, using Oauth and GuzzleHttp.
I'm basing my example off of this Twitter example:
https://github.com/guzzle/oauth-subscriber
My code so far is:
use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Client;
$client = new Client(array(
'base_url' => 'http://magento',
'defaults' => array('auth' => 'oauth')
));
$oauth = new Oauth1(array(
'consumer_key' => 'magento_key',
'consumer_secret' => 'magento_secret'
));
$client->getEmitter()->attach($oauth);
$response = $client->post('/oauth/initiate');
print_r($response);die();
My host is http://magento. I'm trying to POST my key and secret to /oauth/initiate like is says in the Magento docs.
http://www.magentocommerce.com/api/rest/authentication/oauth_authentication.html#OAuthAuthentication-AuthenticationEndpoints
Does anyone have any insight on what I might be doing wrong? I'm getting a 500 error using Firefox's RESTClient to make this call.
EDIT:
I'm now getting this exception thrown
{
"response": "Client error response [url] http://magento/oauth/initiate [status code] 400 [reason phrase] Bad Request"
}
It's getting a 400 from http://magento/oauth/initiate but this is only when using GuzzleHttp and Oauth. If I make the POST directly within RESTClient as http://magento/oauth/initiate, I get an error response from Magento itself (so it does not 400), and if I add all the necessary params I get the appropriate token responses.
UPDATE:
After inspecting the exception being thrown I'm seeing my authorization array does not conain my callback url. This is suppose to be http://magento/oauth/authorize which I've adde in the RESTClient and it works with the token response. Iv'e tried adding this in my $oauth array as 'callback' and 'oauth_callback' but neither seem to work. I can see this appears in the exception, under my data array, but not in the [authorize] array which I believe it needs to be. I tihnk that's what's missing, if someone has an idea of where it needs to be placed.

HTTP 417 error (Expectation Failed) while posting file to web server via Guzzle

I am using Guzzle in Laravel 4 to send file to the remote server which the server will then process. But while posting the file to the server I'm getting the following exception occurs:
Guzzle \ Http \ Exception \ ClientErrorResponseException
Client error response [status code] 417 [reason phrase] Expectation Failed [url] http://example.com/.....
Following is the code that I am using:
use Guzzle\Service\Client as GuzzleClient;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$remote_url = 'http://example.com/';
$client = new GuzzleClient($remote_url);
$client->setSslVerification(FALSE);
$cookieJar = new ArrayCookieJar();
// Create a new cookie plugin
$cookiePlugin = new CookiePlugin($cookieJar);
// Add the cookie plugin to the client
$client->addSubscriber($cookiePlugin);
$post_data = array(
'username' => $input['username'],
'password' => $input['password'],
);
$response = $client->post('login', array(), $post_data)->send();
$response_json = $response->json();
if (isset($response_json['error'])) {
throw new Exception($response_json['error']);
}
$current_time = date("Y-m-d-H-i-s");
$file = 'C:\test\test_file.zip';
$request = $client
->post('receiveFile')
->addPostFields(array('current_time'=>$current_time))
->addPostFile('file', $file)
->send();
The authentication of user seems to work fine and the problem starts only when trying to send the file.
The application throws the error only when I'm trying to send the file to the web server. When I try to send the same file to the same application on my local server, I'm getting the results as I expected without any errors.
I looked for similar problems other people might have faced and found one here on SO Posting a file to a web service with Guzzle , but the solution that worked for the OP of that question didn't work for me. What can I do to solve this problem?
It turned out that when sending the request, a Expect header is added to the request. So what I did was remove the Expect header before sending the request, and everything is working as it should. Following is the code that I changed:
$request = $client
->post('receiveFile')
->addPostFields(array('current_time'=>$current_time))
->addPostFile('file', $file)
->removeHeader('Expect')
->send();
I used the removeHeader method to remove the Expect header. Looks like the removeHeader method must be called just before using the send method, because I had used it before the post method and it hadn't worked before.

Categories