guzzle / oauth-subscriber

Signs Guzzle requests using OAuth 1.0 (Guzzle 6+)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Guzzle 401 response sending data by post

Fynkymynky opened this issue · 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

bump

+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 @Fynkymynky welcome from the future. :)
How did you deal with this problem?

commented

anyone managed to find a solution for this issue?

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