编程语言
首页 > 编程语言> > php – Tumblr OAuth – 缺少或无效的请求令牌

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

作者:互联网

通过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();

解决方法:

Doh,最后它有点简单,请求令牌和秘密请求令牌在connect.php中生成,需要保存以便在callback.php中访问

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

完整代码如下

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";

标签:tumblr,php,api,oauth
来源: https://codeday.me/bug/20190830/1769779.html