如何从tumblr的官方php客户端获取access_token?

Oct*_*pus 2 php oauth tumblr

我已按照此stackoverflow问题中发布的指示,但我被卡住了.

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

我也在这里遵循指示(实际上是推特),但这些指示并不适合我正在使用的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 );
Run Code Online (Sandbox Code Playgroud)

这给了我:

oauth_token=2C6f...MqSF&oauth_token_secret=HaGh...IJLi&oauth_callback_confirmed=true
Run Code Online (Sandbox Code Playgroud)

然后我将用户发送到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 );
Run Code Online (Sandbox Code Playgroud)

因为我得到这样的答复:

oauth_signature [AqbbYs0XSZ7plqB0V3UQ6O6SCVI=] does not match expected value [0XwhYMWswlRWgcr6WeA7/RrwrhA=]
Run Code Online (Sandbox Code Playgroud)

我的最后一步出了什么问题?

我不确定我是否应该发送oauth_verifier请求.是#_=_应该的一部分oauth_verifier?我不这么认为.对于我尝试的所有变体,我都会收到签名错误.

如果没有令牌和tokenSecret,我无法对API进行某些调用.我收到未经授权的403回复.当我从第二步使用token和token_secret时也一样.我很确定我需要一个新的令牌/秘密对.

Joh*_*ohn 7

你是八九不离十,你只是被错误地传递最后一步的oauth_token,并在oauth_token_secret altogeter跳跃出来.

我编译了这个工作代码(您现在也可以在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";
Run Code Online (Sandbox Code Playgroud)