Tumblr OAuth – Missing or invalid request token

两盒软妹~` 提交于 2019-12-05 02:47:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!