Created
March 2, 2016 00:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require-dev": { | |
"symfony/var-dumper": "^3.0" | |
}, | |
"require": { | |
"guzzlehttp/guzzle": "~6", | |
"guzzlehttp/oauth-subscriber": "^0.3.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Content should be..
{
"consumer_key" :"",
"consumer_secret" : "",
"token" : "",
"token_secret" : ""
}
Very old comment but maybe it can save others some time : )
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
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 a401 Unauthorized
response: {"code":"json_oauth1_consumer_mismatch","message":"Token is not registered for the given consumer","data":{"status":401} (truncated...)