forked from daviddesberg/PHPoAuthLib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented missing Harvest stuff and added example
- Loading branch information
Marc Neuhaus
committed
Aug 19, 2014
1 parent
2d0da3d
commit 2dafb0a
Showing
3 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* Example of retrieving an authentication token of the harvest service | ||
* | ||
* PHP version 5.4 | ||
* | ||
* @author David Desberg <[email protected]> | ||
* @author Pieter Hordijk <[email protected]> | ||
* @copyright Copyright (c) 2012 The authors | ||
* @license http://www.opensource.org/licenses/mit-license.html MIT License | ||
*/ | ||
|
||
use OAuth\Common\Consumer\Credentials; | ||
use OAuth\Common\Storage\Session; | ||
use OAuth\Common\Token\Exception\ExpiredTokenException; | ||
use OAuth\OAuth2\Service\Harvest; | ||
|
||
/** | ||
* Bootstrap the example | ||
*/ | ||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
$serviceName = 'Harvest'; | ||
$scopes = array(); | ||
|
||
// Session storage | ||
$storage = new Session(); | ||
|
||
// Setup the credentials for the requests | ||
$credentials = new Credentials( | ||
$servicesCredentials['harvest']['key'], | ||
$servicesCredentials['harvest']['secret'], | ||
$currentUri->getAbsoluteUri() | ||
); | ||
|
||
// Instantiate the Harvest service using the credentials, http client and storage mechanism for the token | ||
/** @var $harves Harves */ | ||
$harvest = $serviceFactory->createService($serviceName, $credentials, $storage, $scopes); | ||
|
||
if (!empty($_GET['clearToken'])) { | ||
// Clear the current AccessToken and go back to the Beginning. | ||
$storage->clearToken($serviceName); | ||
header('Location: ' . $currentUri->getAbsoluteUri()); | ||
|
||
} elseif ($storage->hasAccessToken($serviceName)) { | ||
// fetch the accessToken for the service | ||
$accessToken = $storage->retrieveAccessToken($serviceName); | ||
|
||
// is the accessToken expired? then let's refesh it! | ||
if ($accessToken->isExpired() === TRUE) { | ||
$harvest->refreshAccessToken($accessToken); | ||
} | ||
|
||
// use the service with the valid access token to fetch my email | ||
$result = json_decode($harvest->request('account/who_am_i'), true); | ||
echo 'The email on your harvest account is ' . $result['user']['email']; | ||
|
||
$url = $currentUri->getRelativeUri() . '?clearToken=1'; | ||
echo " <a href='$url'>Click here to clear the current access token</a>"; | ||
|
||
} elseif (!empty($_GET['code'])) { | ||
// This was a callback request from harvest, get the token | ||
$harvest->requestAccessToken($_GET['code']); | ||
header('Location: ' . $currentUri->getAbsoluteUri()); | ||
|
||
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') { | ||
// Redirect to the Authorization uri | ||
$url = $harvest->getAuthorizationUri(); | ||
header('Location: ' . $url); | ||
} else { | ||
$url = $currentUri->getRelativeUri() . '?go=go'; | ||
echo "<a href='$url'>Login with Harvest!</a>"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters