get oauth token with tumblrs official php client

家住魔仙堡 提交于 2019-12-03 12:57:51

问题


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

I know the consumer key and secret but how do I get the token and token secret with tumblrs php client?

I also know the process of oauth but I don't know how to actually implement it :/


回答1:


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.

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

You should get this in $result:

oauth_token=ulE1EuaZvJSN0qIKfQO5EFgcrxrOLJF0Cnm7VbLQqj66oF9nwt&oauth_token_secret=PLjC7s4JeIlgm53q7FKL1wqQkFoL0775JC6UkHKiepAQ6TxXxp&oauth_callback_confirmed=true

See this commit in Github for more info on how this was made possible.



来源:https://stackoverflow.com/questions/18176109/get-oauth-token-with-tumblrs-official-php-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!