Skip to content

Instantly share code, notes, and snippets.

@duncanjbrown
Created March 2, 2016 00:05
Show Gist options
  • Save duncanjbrown/9e141444a85a3e59f91b to your computer and use it in GitHub Desktop.
Save duncanjbrown/9e141444a85a3e59f91b to your computer and use it in GitHub Desktop.
"WP API and OAuth – Using WordPress without WordPress": updated code for Guzzle 6 and latest WP-API/Oauth1
<?php
require_once 'vendor/autoload.php';
session_start();
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
$token = $_GET['oauth_token'];
$oauth_verifier = $_GET['oauth_verifier'];
$stack = HandlerStack::create();
$consumer_credentials = json_decode( file_get_contents( 'consumer.json' ), true );
$middleware = new Oauth1( array_merge( $consumer_credentials, [
'token' => $_SESSION['oauth_token'],
'token_secret' => $_SESSION['oauth_token_secret']
] ) );
$stack->push($middleware);
$client = new Client([
'base_uri' => 'http://localhost:8080/',
'handler' => $stack,
'auth' => 'oauth'
]);
try {
$req = $client->post('oauth1/access', ['form_params' => [ 'oauth_verifier' => $oauth_verifier ] ]);
$params = (string)$req->getBody();
parse_str($params);
$credentials = [
'consumer_key' => 'ONptPZtywAbn',
'consumer_secret' => 'MvuODbEx6Fyhwb0eBF5t9fulrcwDuCSJUDE8FmYfkNxyMf3k',
'oauth_token' => $oauth_token,
'oauth_token_secret' => $oauth_token_secret
];
file_put_contents( 'access.json', json_encode( $credentials ) );
echo '<h2>All done!</h2> <p>Credentials below, also saved to oauth-submitted/access.json.</p>';
echo '<pre>'.json_encode( $credentials ).'</pre>';
} catch (ClientException $e) {
dump((string)$e->getResponse()->getBody());
} catch (\Exception $e) {
dump($e);
}
{
"require-dev": {
"symfony/var-dumper": "^3.0"
},
"require": {
"guzzlehttp/guzzle": "~6",
"guzzlehttp/oauth-subscriber": "^0.3.0"
}
}
<?php
require_once 'vendor/autoload.php';
session_start();
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
$stack = HandlerStack::create();
$consumer_credentials = json_decode( file_get_contents( 'consumer.json' ), true );
$middleware = new Oauth1($consumer_credentials);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'http://localhost:8080/',
'handler' => $stack,
'auth' => 'oauth'
]);
$callback = 'http://localhost:3030/callback.php';
$res = $client->post('http://localhost:8080/oauth1/request', [ 'form_params' => ['oauth_callback' => $callback]]);
try {
parse_str($res->getBody());
$_SESSION['oauth_token'] = $oauth_token;
$_SESSION['oauth_token_secret'] = $oauth_token_secret;
header("Location: http://localhost:8080/oauth1/authorize?oauth_token={$oauth_token}&oauth_token_secret={$oauth_token_secret}");
} catch (\Exception $e) {
dump($e);
}
@chadsterBAM
Copy link

consumer.json ?? what is in this file? Not exactly, but what are the parameters?

I can't get mine working no matter what I try. Though I am building it using Laravel (5), GuzzleHTTP Client, HandlerStack and Subscriber\Oauth\Oauth1.

GET https://domain/wp-json/wp/v2/blogs/user resulted in a 401 Unauthorized response: {"code":"json_oauth1_consumer_mismatch","message":"Token is not registered for the given consumer","data":{"status":401} (truncated...)

@ricemann
Copy link

ricemann commented Oct 8, 2016

Content should be..

{
"consumer_key" :"",
"consumer_secret" : "",
"token" : "",
"token_secret" : ""
}

Very old comment but maybe it can save others some time : )

@ricemann
Copy link

ricemann commented Oct 8, 2016

However - this isn't working with wordpress anymore - callback.php needs: Missing OAuth parameters oauth_timestamp, oauth_nonce, oauth_signature, oauth_signature_method which isn't provided by /oauth1/authorize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment