carlos-mg89 /
PHPoAuthLib
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Example of retrieving an authentication token of the Tumblr service. |
||
| 5 | * |
||
| 6 | * PHP version 5.4 |
||
| 7 | * |
||
| 8 | * @author David Desberg <[email protected]> |
||
| 9 | * @author Pieter Hordijk <[email protected]> |
||
| 10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 11 | */ |
||
| 12 | |||
| 13 | use OAuth\Common\Consumer\Credentials; |
||
| 14 | use OAuth\Common\Storage\Session; |
||
| 15 | use OAuth\OAuth1\Service\Tumblr; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Bootstrap the example. |
||
| 19 | */ |
||
| 20 | require_once __DIR__ . '/bootstrap.php'; |
||
| 21 | |||
| 22 | // We need to use a persistent storage to save the token, because oauth1 requires the token secret received before' |
||
| 23 | // the redirect (request token request) in the access token request. |
||
| 24 | $storage = new Session(); |
||
| 25 | |||
| 26 | // Setup the credentials for the requests |
||
| 27 | $credentials = new Credentials( |
||
| 28 | $servicesCredentials['tumblr']['key'], |
||
| 29 | $servicesCredentials['tumblr']['secret'], |
||
| 30 | $currentUri->getAbsoluteUri() |
||
| 31 | ); |
||
| 32 | |||
| 33 | // Instantiate the tumblr service using the credentials, http client and storage mechanism for the token |
||
| 34 | /** @var Tumblr $tumblrService */ |
||
| 35 | $tumblrService = $serviceFactory->createService('tumblr', $credentials, $storage); |
||
| 36 | |||
| 37 | if (!empty($_GET['oauth_token'])) { |
||
| 38 | $token = $storage->retrieveAccessToken('Tumblr'); |
||
| 39 | |||
| 40 | // This was a callback request from tumblr, get the token |
||
| 41 | $tumblrService->requestAccessToken( |
||
| 42 | $_GET['oauth_token'], |
||
| 43 | $_GET['oauth_verifier'], |
||
| 44 | $token->getRequestTokenSecret() |
||
| 45 | ); |
||
| 46 | |||
| 47 | // Send a request now that we have access token |
||
| 48 | $result = json_decode($tumblrService->request('user/info')); |
||
| 49 | |||
| 50 | echo 'result: <pre>' . print_r($result, true) . '</pre>'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 51 | } elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { |
||
| 52 | // extra request needed for oauth1 to request a request token :-) |
||
| 53 | $token = $tumblrService->requestRequestToken(); |
||
| 54 | |||
| 55 | $url = $tumblrService->getAuthorizationUri(['oauth_token' => $token->getRequestToken()]); |
||
| 56 | header('Location: ' . $url); |
||
| 57 | } else { |
||
| 58 | $url = $currentUri->getRelativeUri() . '?go=go'; |
||
| 59 | echo "<a href='$url'>Login with Tumblr!</a>"; |
||
| 60 | } |
||
| 61 |