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

2024-03-24

我已按照中发布的说明进行操作这个计算器问题 https://stackoverflow.com/questions/18176109/get-oauth-token-with-tumblrs-official-php-client,但我被困住了。

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

我也在遵循指示here https://dev.twitter.com/docs/auth/implementing-sign-twitter(实际上是针对 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 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?我不这么认为。我尝试过的所有变体都出现签名错误。

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


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

我已经编译了这个工作代码(您现在也可以在 Wiki 上找到该代码)https://github.com/tumblr/tumblr.php/wiki/Authentication 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";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 tumblr 官方 php 客户端获取 access_token? 的相关文章

随机推荐