Skip to content

Instantly share code, notes, and snippets.

@vitor-mariano
Created September 12, 2015 13:57
Show Gist options
  • Save vitor-mariano/038e2296005e5fd45225 to your computer and use it in GitHub Desktop.
Save vitor-mariano/038e2296005e5fd45225 to your computer and use it in GitHub Desktop.
How to use GuzzleHttp as a Tumblr client.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Subscriber\Oauth\Oauth1 as OAuth;
use App\Http\Controllers\Controller;
class AppController extends Controller
{
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function getAuthorize()
{
$stack = HandlerStack::create();
$middleware = new OAuth([
'consumer_key' => env('TUMBLR_CONSUMER_KEY'),
'consumer_secret' => env('TUMBLR_CONSUMER_SECRET'),
'token' => '',
'token_secret' => '',
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://www.tumblr.com/oauth/',
'handler' => $stack,
'auth' => 'oauth',
]);
$response = $client->post('request_token', [
'form_params' => ['oauth_callback' => route('app.authorized')],
]);
parse_str((string) $response->getBody(), $tokens);
foreach (['oauth_token', 'oauth_token_secret'] as $token) {
$this->request->session()->flash($token, $tokens[$token]);
}
return view('app.authorize')->with('oauthToken', $tokens['oauth_token']);
}
public function getAuthorized()
{
$stack = HandlerStack::create();
$middleware = new OAuth([
'consumer_key' => env('TUMBLR_CONSUMER_KEY'),
'consumer_secret' => env('TUMBLR_CONSUMER_SECRET'),
'token' => $this->request->session()->get('oauth_token'),
'token_secret' => $this->request->session()->get('oauth_token_secret'),
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://www.tumblr.com/oauth/',
'handler' => $stack,
'auth' => 'oauth',
]);
try {
$response = $client->post('access_token', [
'form_params' => [
'oauth_verifier' => $this->request->input('oauth_verifier'),
],
]);
} catch (ClientException $e) {
return view('app.unauthorized');
}
parse_str((string) $response->getBody(), $tokens);
dd($tokens);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment