【问题标题】:How do I get the access_token from tumblr's official php client?如何从 tumblr 的官方 php 客户端获取 access_token?
【发布时间】:2013-11-27 06:12:23
【问题描述】:

我已按照this stackoverflow question 中发布的说明进行操作,但我被卡住了。

我正在使用来自 Github 的 tumblr/tumblr.php(官方“tumblr API 的 PHP 客户端”)。

我也遵循here 的指示(实际上是针对 twitter 的),但这些指示并不是为我正在使用的 git 库量身定制的。

我有一个有效的消费者密钥和秘密。

从我提出请求并像这样获得 oauth_token 和 oauth_token_secret 的人:

$client = new Tumblr\API\Client($consumerKey,$consumerSecret);
$client->getRequestHandler()->setBaseUrl('https://www.tumblr.com/');
$req = $client->getRequestHandler()->request('POST', 'oauth/request_token', [
  'oauth_callback' => '...',
]);
// Get the result
$result = $req->body->__toString();
print_r( $result );

这给了我:

oauth_token=2C6f...MqSF&oauth_token_secret=HaGh...IJLi&oauth_callback_confirmed=true

然后我将用户发送到http://www.tumblr.com/oauth/authorize?oauth_token=2C6f...MqSF,以便他们允许访问该应用程序。这会重定向到:...?oauth_token=2C6f...MqSF&oauth_verifier=nvjl...GtEa#_=_

现在在最后一步,我相信我应该将我的请求令牌转换为访问令牌。是对的吗?我做错了什么:

$client = new Tumblr\API\Client($consumerKey,$consumerSecret);
$client->getRequestHandler()->setBaseUrl('https://www.tumblr.com/');
$req = $client->getRequestHandler()->request('POST', 'oauth/access_token', [
  'oauth_token' => '2C6f...MqSF',
  'oauth_verifier' => 'nvjl...GtEa'
]);
// Get the result
$result = $req->body->__toString();
print_r( $result );

因为我收到了这样的回复:

oauth_signature [AqbbYs0XSZ7plqB0V3UQ6O6SCVI=] does not match expected value [0XwhYMWswlRWgcr6WeA7/RrwrhA=]

我的最后一步有什么问题?

我不确定我是否应该在请求中发送oauth_verifier#_=_ 应该是 oauth_verifier 的一部分吗?我不会这么认为。我尝试过的所有变体都有签名错误。

没有 token 和 tokenSecret 我无法对 API 进行某些调用。我收到未经授权的 403 响应。当我使用第二步中的 token 和 token_secret 时也是如此。我很确定我需要一个新的令牌/秘密对。

【问题讨论】:

    标签: php oauth tumblr


    【解决方案1】:

    你已经很接近了,你只是在最后一步错误地传递了 oauth_token,并跳过了 oauth_token_secret altogeter。

    我已经编译了这个工作代码(您现在也可以在 Wiki 上的 https://github.com/tumblr/tumblr.php/wiki/Authentication 上找到它):

    <?php
    
    require_once('vendor/autoload.php');
    
    // some variables that will be pretttty useful
    $consumerKey = '<your consumer key>';
    $consumerSecret = 'your consumer secret>';
    $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);
    
    // tell the user where to go
    echo 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
    $client->setToken($data['oauth_token'], $data['oauth_token_secret']);
    
    // get the verifier
    echo "\noauth_verifier: ";
    $handle = fopen('php://stdin', 'r');
    $line = fgets($handle);
    
    // exchange the verifier for the keys
    $verifier = trim($line);
    $resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
    $out = $result = $resp->body;
    $data = array();
    parse_str($out, $data);
    
    // 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";
    

    【讨论】:

    • 从哪里获得 vendor/autoload.php? Tumblr API 中没有这样的文件。
    猜你喜欢
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2020-08-22
    • 2021-03-30
    • 2022-06-15
    • 2020-08-02
    • 2021-10-19
    相关资源
    最近更新 更多