使用 tumblrs 官方 php 客户端获取 oauth 令牌 [英] get oauth token with tumblrs official php client
问题描述
这是我第一次使用 api 和 oauth,tumblr 有一个 php 客户端.我已经用 composer 下载并安装了客户端.这是他们必须设置客户端的代码.
This is my first time playing with an api and oauth and tumblr has a php client. I have downloaded and installed the client with composer. This is the code they have to set up the client.
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client->setToken($token, $tokenSecret);
我知道消费者密钥和秘密,但如何使用 tumblrs php 客户端获取令牌和令牌秘密?
I know the consumer key and secret but how do I get the token and token secret with tumblrs php client?
我也知道 oauth 的过程,但我不知道如何实际实现它:/
I also know the process of oauth but I don't know how to actually implement it :/
推荐答案
就这样我们在同一个页面中,您可以通过浏览器登录流程舞蹈来获取用户的令牌和秘密.Tumblr 的流程与 Twitter 的流程几乎相同,因此您可以将其用作参考:Implementing Sign在推特上.您可以查看 Tumblr 的身份验证文档中的 OAuth 部分 以获得正确的端点.
Just so we're in the same page, you can get the user's token and secret by going through the browser sign-in flow dance. Tumblr's flow is pretty much the same as Twitter's so you can use this as reference: Implementing Sign in with Twitter. You can look at the OAuth part in Tumblr's Authentication documentation to get the correct endpoints.
请注意,您链接到的 Tumblr 的 PHP 客户端具有默认基本网址 设置为 "http://api.tumblr.com/"
而 OAuth 端点(例如 request_token
)使用 "http://www.tumblr.com"
.为了能够使用 OAuth 端点,您只需更改基本 url.以下是登录流程第一步的示例,即获取请求令牌:
Note that Tumblr's PHP client that you linked to has the default base url set to "http://api.tumblr.com/"
whereas the OAuth endpoints (e.g. request_token
) use "http://www.tumblr.com"
. To be able to use the OAuth endpoints, you will just have to change the base url. Here's an example of the first step in the sign-in flow, getting a request token:
// Requesting for http://www.tumblr.com/oauth/request_token
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
// Change the base url
$client->getRequestHandler()->setBaseUrl('http://www.tumblr.com/');
$req = $client->getRequestHandler()->request('POST', 'oauth/request_token', [
'oauth_callback' => '...',
]);
// Get the result
$result = $req->body->__toString();
你应该在 $result
中得到这个:
You should get this in $result
:
oauth_token=ulE1EuaZvJSN0qIKfQO5EFgcrxrOLJF0Cnm7VbLQqj66oF9nwt&oauth_token_secret=PLjC7s4JeIlgm53q7FKL1wqQkFoL0775JC6UkHKiepAQ6TxXxp&oauth_callback_confirmed=true
请参阅 Github 中的 commit,了解有关如何实现这一点的更多信息.
See this commit in Github for more info on how this was made possible.
这篇关于使用 tumblrs 官方 php 客户端获取 oauth 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!