如何从 tumblr 的官方 php 客户端获取 access_token? [英] How do I get the access_token from tumblr's official php client?

查看:114
本文介绍了如何从 tumblr 的官方 php 客户端获取 access_token?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照这个stackoverflow问题,但我卡住了.

I've followed the directions posted in this stackoverflow question, but I am stuck.

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

I am using tumblr/tumblr.php from Github (the official "PHP client for the tumblr API").

我也在遵循这里(实际上是为了twitter),但这些说明不是为我正在使用的 git 库量身定制的.

I am also following directions here (which are actually for twitter), but those directions aren't tailored for the git library I am using.

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

I have a valid consumer key and secret.

从那些我提出请求并获得 oauth_token 和 oauth_token_secret 的人那里:

From those I make a request and get oauth_token and oauth_token_secret like so:

$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#_=_

Then I send the user to the http://www.tumblr.com/oauth/authorize?oauth_token=2C6f...MqSF, so they can allow access for the app. This redirects to: ...?oauth_token=2C6f...MqSF&oauth_verifier=nvjl...GtEa#_=_

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

And now in the final step I believe I am supposed to convert my request token to an access token. Is that right? I am doing something wrong:

$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 );

因为我收到这样的回复:

because I get responses like this one:

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

<小时>

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

我不确定我是否应该随请求一起发送 oauth_verifier.#_=_ 是否应该成为 oauth_verifier 的一部分?我不会这么认为.我尝试过的所有变体都出现签名错误.

I am not sure if I should even be sending oauth_verifier with the request. Is #_=_ supposed to be part of oauth_verifier? I wouldn't think so. I get signature errors for all the variations Ive tried.

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

Without the token and tokenSecret I can't make certain calls to the API. I get unauthorized 403 responses. Same when I use the token and token_secret from the second step. I'm pretty sure I need a new token/secret pair.

推荐答案

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

You're pretty close, you just are passing the oauth_token incorrectly in the last step, and skipping out on oauth_token_secret altogeter.

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

I've compiled this working code (which you can also now find posted on the Wiki at 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";

这篇关于如何从 tumblr 的官方 php 客户端获取 access_token?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆