问题
Having some problems with authorizing against Tumblrs API via OAuth.
This is what's going on in my callback.php
:
require_once('../vendor/autoload.php');
// some variables that will be pretttty useful
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// Fetch the current URL which holds important data
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// get the verifier value
$verifier = $_GET['oauth_verifier'];
// exchange the verifier for the keys
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
echo '<pre>';
var_dump($data);
echo '</pre>';
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
echo "\ntoken: " . $token . "\nsecret: " . $secret;
// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "\ncongrats " . $info->user->name . "!\n";
Dumping out $data gives me this:
array(1) {
["Missing_or_invalid_request_token_"]=>
string(0) ""
}
What am I missing?
Not sure if it's important, but for reference for future endeavorers my initial connect.php
looks like this:
require_once('../vendor/autoload.php');
// some variables that will be pretttty useful
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// start the old gal up
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
// get the oauth_token
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
if($data['oauth_callback_confirmed']) {
// redirect
$url = 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
header('Location: '.$url);
} else {
echo 'Could not connect to Tumblr. Refresh the page or try again later.';
}
exit();
回答1:
Doh, in the end it was kind of simple, request token and secret request token is generated in connect.php
and needs to be saved for access in callback.php
This is solved simply with session variables. Since I haven't seen any working examples on how to implement this with the official Tumblr PHP clients, I hope this will be of benefit for others.
Full code below
connect.php
session_start();
require_once('../vendor/autoload.php');
// some variables that will be pretttty useful
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// start the old gal up
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
// get the oauth_token
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
$_SESSION['request_token'] = $data['oauth_token'];
$_SESSION['request_token_secret'] = $data['oauth_token_secret'];
if($data['oauth_callback_confirmed']) {
// redirect
$url = 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
header('Location: '.$url);
} else {
echo 'Could not connect to Tumblr. Refresh the page or try again later.';
}
exit();
callback.php
session_start();
require_once('../vendor/autoload.php');
// some variables that will be pretttty useful
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $_SESSION['request_token'], $_SESSION['request_token_secret']);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
unset($_SESSION['request_token']);
unset($_SESSION['request_token_secret']);
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$verifier = $_GET['oauth_verifier'];
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
echo '<pre>';
var_dump($data);
echo '</pre>';
// and print out our new keys
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
echo "\ntoken: " . $token . "\nsecret: " . $secret;
// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "\ncongrats " . $info->user->name . "!\n";
来源:https://stackoverflow.com/questions/24630354/tumblr-oauth-missing-or-invalid-request-token