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
You should use the
withMiddleware
function to push$middleware
onto the stack.You will also need to use
withOptions
to set'auth' => 'oauth'
Do you have an example cause this does not work => 401 Unauthorized :