Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explaining the "basic usage" a little bit more. #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 65 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,79 @@ The official PHP client for the

## Usage

### Basic Usage
### Authentication

The first step is setting up a Client:
This client allows you to use one of these authentication levels:

- API key
- OAuth

Using API key, the simplest one, you just need to set the client with you Consumer Key, provided by [Tumblr API](https://www.tumblr.com/oauth/apps), like this
``` php
$client = new Tumblr\API\Client($consumerKey);
```
for use methods which are accessible from the API key. E.g.
``` php
$client->getBlogInfo($blogName);
```
The OAuth level is a little more complex, because it gives more access to user account. You need tokens which are provided only by user authorization. To obtain these tokens, first you need to:

- Guarantee that your application credentials are valid
- Tell which page should the user return to
- Redirect user to the Tumblr authorization page

So let's consider you are coding the page `https://example.com/auth/tumblr`. You must configure your client with your application credentials, provided by [OAuth](https://www.tumblr.com/oauth/apps)
``` php
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client->setToken($token, $tokenSecret);
```
Point the request handler to Tumblr
``` php
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
```
And then send the request to Tumblr with your callback URL. Let's consider it would be `https://example.com/auth/tumblr/callback`.
``` php
$response = $requestHandler->request('POST', 'oauth/request_token', [
'oauth_callback' => 'https://example.com/auth/tumblr/callback'
]);
```
If your credentials are valid, you should receive temporary tokens to continue. You may extract them this way
``` php
parse_str((string) $response->body, $tokens);
```
`$tokens` will contain
``` php
['oauth_token' => '...', 'oauth_token_secret' => '...']
```
Save these tokens somehow (e.g. using `$_SESSION`), because we're going to use them in the next session. Now, the user must be redirected to URL `https://www.tumblr.com/oauth/authorize?oauth_token={$tokens['oauth_token']}`.

And then you can do anything you'd like:
Now, the user should decide if authorizes your application and then should be redirected to your callback page with 'get' param `oauth_verifier`. Now let's consider you're coding the page `https://example.com/auth/tumblr/callback`. One more time you should set the client with your application credentials and termporary tokens stored in the last session.
``` php
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
```
Again let's point the request handler to Tumblr
``` php
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
```
And send the request to Tumblr with param `oauth_verifier`, at this time receiving the definitive tokens
``` php
$response = $requestHandler->request('POST', 'oauth/access_token', [
'oauth_verifier' => $oauthVerifier
]);
```
You can also use the variable `$_GET` to recover `$oauthVerifier`.

If everything runs correctly, you should receive the definitive tokens, which may be extracted this way
``` php
parse_str((string) $response->body, $tokens);
```
Remember: you can verify the response status with `$response->status`. If everything runs correctly, the status will be `200`. Otherwise, will be `401`. You can see all status [here](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).

Finally, you can use any method provided by client.
``` php
foreach ($client->getUserInfo()->user->blogs as $blog) {
echo $blog->name . "\n";
}
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
$client->getUserInfo();
```

### User Methods
Expand Down