forked from logical-and/php-oauth
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathharvest.php
More file actions
76 lines (63 loc) · 2.46 KB
/
harvest.php
File metadata and controls
76 lines (63 loc) · 2.46 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Example of retrieving an authentication token of the harvest service
*
* PHP version 5.4
*
* @author And <and.webdev@gmail.com>
* @author David Desberg <david@daviddesberg.com>
* @author Pieter Hordijk <info@pieterhordijk.com>
* @copyright Copyright (c) 2015 The authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Url;
use OAuth\Common\Storage\Session;
use OAuth\OAuth2\Service\Harvest;
/**
* Bootstrap the example
*/
require_once __DIR__ . '/bootstrap.php';
$serviceName = 'Harvest';
$scopes = [];
// Session storage
$storage = new Session();
/** @var Url $currentUri */
// Setup the credentials for the requests
$credentials = new Credentials(
$servicesCredentials[ 'harvest' ][ 'key' ],
$servicesCredentials[ 'harvest' ][ 'secret' ],
$currentUri
);
// Instantiate the Harvest service using the credentials, http client and storage mechanism for the token
/** @var $harvest Harvest */
$harvest = $serviceFactory->createService($serviceName, $credentials, $storage, $scopes);
if (!empty($_GET[ 'clearToken' ])) {
// Clear the current AccessToken and go back to the Beginning.
$storage->clearToken($serviceName);
$currentUri->setQuery('');
header('Location: ' . $currentUri);
} elseif ($storage->hasAccessToken($serviceName)) {
// fetch the accessToken for the service
$accessToken = $storage->retrieveAccessToken($serviceName);
// is the accessToken expired? then let's refresh it!
if ($accessToken->isExpired() === true) {
$harvest->refreshAccessToken($accessToken);
}
// use the service with the valid access token to fetch my email
$result = $harvest->requestJSON('account/who_am_i');
echo 'The email on your harvest account is ' . $result[ 'user' ][ 'email' ];
echo '<br />';
echo 'Your extracted username is a: ' . $harvest->constructExtractor()->getUsername();
echo '<br />';
$url = $currentUri . '?clearToken=1';
echo " <a href='$currentUri'>Click here to clear the current access token</a>";
} elseif ($harvest->isGlobalRequestArgumentsPassed()) {
// This was a callback request from harvest, get the token
$harvest->retrieveAccessTokenByGlobReqArgs();
header('Location: ' . $currentUri);
} elseif (!empty($_GET[ 'go' ]) && $_GET[ 'go' ] === 'go') {
$harvest->redirectToAuthorizationUri();
} else {
echo "<a href='$url?go=go'>Login with Harvest!</a>";
}