summaryrefslogtreecommitdiff
path: root/vendor/lusitanian/oauth/examples/pocket.php
blob: b96d2aceaddbeee12b1d84536773523a8ea63aad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

/**
 * Example of retrieving an authentication token of the Pocket service.
 *
 * @author     Christian Mayer <thefox21at@gmail.com>
 * @copyright  Copyright (c) 2014 Christian Mayer <thefox21at@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 */

use OAuth\OAuth2\Service\Pocket;
use OAuth\Common\Storage\Session;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Client\CurlClient;

/**
 * Bootstrap the example
 */
require_once __DIR__.'/bootstrap.php';

$step = isset($_GET['step']) ? (int)$_GET['step'] : null;
$code = isset($_GET['code']) ? $_GET['code'] : null;

// Session storage
$storage = new Session();

// Setup the credentials for the requests
$credentials = new Credentials(
	$servicesCredentials['pocket']['key'],
	null, // Pocket API doesn't have a secret key. :S
	$currentUri->getAbsoluteUri().($code ? '?step=3&code='.$code : '')
);

// Instantiate the Pocket service using the credentials, http client and storage mechanism for the token
$pocketService = $serviceFactory->createService('Pocket', $credentials, $storage);

switch($step){
	default:
		print '<a href="'.$currentUri->getRelativeUri().'?step=1">Login with Pocket</a>';
		
		break;
	
	case 1:
		$code = $pocketService->requestRequestToken();
		header('Location: '.$currentUri->getRelativeUri().'?step=2&code='.$code);
		
		break;
	
	case 2:
		$url = $pocketService->getAuthorizationUri(array('request_token' => $code));
		header('Location: '.$url);
		
		break;
	
	case 3:
		$token = $pocketService->requestAccessToken($code);
		$accessToken = $token->getAccessToken();
		$extraParams = $token->getExtraParams();
		
		print 'User: '.$extraParams['username'].'<br />';
		print 'Access Token: '.$accessToken;
		break;
}