Tumblr OAuth - 缺少或无效的请求令牌

时间:2014-07-08 11:14:12

标签: php api oauth tumblr

通过OAuth授权Tumblrs API时遇到一些问题 这就是我的 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";

倾销$ data给我这个:

array(1) {
  ["Missing_or_invalid_request_token_"]=>
  string(0) ""
}

我错过了什么?



不确定它是否重要,但作为未来努力人员的参考,我的初始 connect.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/');

// 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 个答案:

答案 0 :(得分:5)

Doh,最后它很简单,请求令牌和秘密请求令牌在 connect.php 中生成,需要保存才能在 {{1}中进行访问}

这可以通过会话变量简单地解决。 由于我还没有看到任何有关如何使用官方Tumblr PHP客户端实现此功能的实例,我希望这对其他人有益。

以下完整代码

<强> connect.php

callback.php

<强> 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);
$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();