skip to Main Content

I’m actually using Guzzle and Guzzle OAuth Subscriber https://github.com/guzzle/oauth-subscriber to post a tweet via the Twitter api.

My code looks like this :

    $stack = HandlerStack::create();

    $middleware = new Oauth1([
        'consumer_key'    => config('services.twitter.client_id'),
        'consumer_secret' => config('services.twitter.client_secret'),
        'token'           => $this->user->twitter->token,
        'token_secret'    => $this->user->twitter->token_secret,
    ]);

    $stack->push($middleware);

    $client = new Client([
        'base_uri' => 'https://api.twitter.com/1.1/',
        'handler' => $stack,
        'auth' => 'oauth',
    ]);

    $status = 'My tweet and the link to tweet...';

    $res = $client->post('https://api.twitter.com/1.1/statuses/update.json', [
        'query'=>[
            'status'=>$status,
            'possibly_sensitive'=>false,
        ]
    ]);

This code works but I don’t know how I can use the OAuth Subscriber with the Laravel 7 HTTP client to do the same work.

Any idea how do that (if possible) ?

2

Answers


  1. You should use the withMiddleware function to push $middleware onto the stack.
    You will also need to use withOptions to set 'auth' => 'oauth'

    Login or Signup to reply.
  2. Do you have an example cause this does not work => 401 Unauthorized :

    $stack = HandlerStack::create();
    
    $middleware = new Oauth1([
        'consumer_key' => config('api.test.consumer_key'),
        'consumer_secret' => config('api.test.consumer_secret'),
        'token'  => config('api.test.token'),
        'token_secret' => config('api.test.token_secret')
    ]);
    
    $stack->push($middleware);
    
    $this->http = Http::withMiddleware($middleware)
        ->withOptions([
            'auth' => 'oauth',
            'base_uri' => config('api.test.url'),
            'handler' => $stack
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search