Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guzzle 401 response sending data by post #31

Open
Fynkymynky opened this issue May 24, 2015 · 6 comments
Open

Guzzle 401 response sending data by post #31

Fynkymynky opened this issue May 24, 2015 · 6 comments

Comments

@Fynkymynky
Copy link

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
Copy link
Author

bump

@carlb-jomedia
Copy link

+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

@NavyCoat
Copy link

NavyCoat commented Dec 6, 2020

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

@grig-fifty
Copy link

anyone managed to find a solution for this issue?

@Urvashi1894
Copy link

bump

@ponaravind-outdoorgoats
Copy link

ponaravind-outdoorgoats commented Jan 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants