oauth-subscriber icon indicating copy to clipboard operation
oauth-subscriber copied to clipboard

Guzzle 401 response sending data by post

Open Fynkymynky opened this issue 9 years ago • 6 comments

I've been using Guzzle to get data from my API, but when I try and post data to the API I get the following error:

Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error response [url] http://www.mywebsite.bar/rest/v4/foo/ [status code] 401 [reason phrase] Unauthorized : Invalid signature

The code I'm using to send the data looks like this

$request = $this->client->post('http://www.mywebsite.bar/rest/v4/foo/' ,
  array('Content-Type: application/x-www-form-urlencoded'),
  array());
$request->setBody($my_data); #set body!
$response = $request->send();

return $response;

Using the same client to get data works fine

$res = $this->client->get( $url);
return $res->getBody();

Using OAuth I've managed to post the data to the API like this

$my_data['name'] = 'Bob';
$my_data['age'] = 34;
$my_data['location[home]'] = 'foo';
$my_data['location[work]'] = 'bar';

$oauth->fetch( $url , $my_data , OAUTH_HTTP_METHOD_POST );

Sending the array to the API this way works fine.

I'm using guzzlehttp/oauth-subscriber for all the oauth side.

What am I doing wrong with Guzzle?

Thanks

Fynkymynky avatar May 24 '15 09:05 Fynkymynky

bump

Fynkymynky avatar Jul 20 '15 16:07 Fynkymynky

+1 I really have a hard time with sending post param with this librairie. Would need some more example. Can't seem to add POST params to the body and get authenticated by an API

carlb-jomedia avatar Oct 05 '15 15:10 carlb-jomedia

@carlb-jomedia @Fynkymynky welcome from the future. :) How did you deal with this problem?

NavyCoat avatar Dec 06 '20 19:12 NavyCoat

anyone managed to find a solution for this issue?

grig-fifty avatar Jan 31 '22 23:01 grig-fifty

bump

Urvashi1894 avatar Jul 06 '22 16:07 Urvashi1894

Hi guys I used git hub copilot to find the solution. To send post request use my code

<?php

use GuzzleHttp\Subscriber\Oauth\Oauth1;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

require_once 'vendor/autoload.php';


$stack = HandlerStack::create();

$middleware = new Oauth1([
    'consumer_key'    => 'xxxxxxxxxxx',
    'consumer_secret' => 'xxxxxxxxxxxxx',
    'token'           => 'xxxxxxxxxxxx',
    'token_secret'    => 'xxxxxxxxxxx',
    'signature_method' => Oauth1::SIGNATURE_METHOD_HMACSHA256,
]);
$stack->push($middleware);

$client = new Client([
    'base_uri' => 'http://wildcountrygear.in/',
    'handler' => $stack,
    'auth' => 'oauth'
]);

$options = [
    'json' => [
        'username' => 'xxxxxxxxxxx',
        'password' => 'xxxxxxxxxxx'
    
    ]
];

// Now you don't need to add the auth parameter
$res = $client->post('http://wildcountrygear.in/rest/default/V1/integration/admin/token',$options);
echo $res->getBody();

ponaravind-outdoorgoats avatar Jan 05 '24 09:01 ponaravind-outdoorgoats