php - Tumblr OAuth – 请求 token 丢失或无效

标签 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) ""
}

我错过了什么?



不确定它是否重要,但为了供 future 的努力者引用,我最初的 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();

最佳答案

呵呵,最后有点简单,request token 和 secret request token 在 connect.php 中生成,需要保存在 中访问>callback.php

这可以通过 session 变量简单地解决。 由于我还没有看到任何有关如何使用官方 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();

回调.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";

关于php - Tumblr OAuth – 请求 token 丢失或无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630354/

相关文章:

php - ckeditor echo php 变量?

点击 PHP "continue"

php - 在 PHP 中将 4 个字节解包为 float

api - Postman API 不会跳过 CSV 的空行

php - 构建一个简单的 RESTful api

php - google-api-php-client:无效的客户端 secret JSON 文件

oauth - OAuth 是否总是假定用户界面?

php - 捕获 GET 或 Retrieve 的 Stripe 错误

azure - Azure 静态 Web 应用中的 Azure Function 的 CORS

node.js - 将 OAuth 与 Kuzzle 集成