public function testObtainAccessTokenShouldSetExpectedState() { GlobalNamespace::reset(array('time' => function ($expectation) { $expectation->andReturn(strtotime('1999-01-01Z')); }, 'curl_exec' => function ($expectation) { $expectation->andReturn(json_encode(self::mockTokenData('NeW'))); })); $client = new Client(array('client_id' => $this->mockClientId(), 'state' => (object) array('redirect_uri' => 'http://te.st/callback', 'token' => null))); $secret = $this->mockClientSecret(); $client->obtainAccessToken($secret, 'X99ffffff-ffff-ffff-ffff-ffffffffffff'); $actual = $client->getState(); $this->assertEquals((object) array('redirect_uri' => null, 'token' => (object) array('obtained' => strtotime('1999-01-01Z'), 'data' => (object) array('token_type' => 'bearer', 'expires_in' => 3600, 'scope' => 'wl.signin wl.basic wl.contacts_skydrive wl.skydrive_update wl.offline_access', 'access_token' => 'NeW/AcCeSs+ToKeN', 'refresh_token' => 'NeW!ReFrEsH*ToKeN', 'authentication_token' => 'NeW.AuThEnTiCaTiOn_ToKeN', 'user_id' => 'ffffffffffffffffffffffffffffffff'))), $actual); }
// not log in successfully or did not grant privileges requested), we cannot // proceed in obtaining an access token. if (!array_key_exists('code', $_GET)) { throw new \Exception('code undefined in $_GET'); } session_start(); // Attempt to load the OneDrive client' state persisted from the previous // request. if (!array_key_exists('onedrive.client.state', $_SESSION)) { throw new \Exception('onedrive.client.state undefined in $_SESSION'); } $onedrive = new Client(array('client_id' => $config['ONEDRIVE_CLIENT_ID'], 'state' => $_SESSION['onedrive.client.state'])); // Obtain the token using the code received by the OneDrive API. $onedrive->obtainAccessToken($config['ONEDRIVE_CLIENT_SECRET'], $_GET['code']); // Persist the OneDrive client' state for next API requests. $_SESSION['onedrive.client.state'] = $onedrive->getState(); } catch (\Exception $e) { $status = sprintf('<p>Reason: <cite>%s</cite></p>', htmlspecialchars($e->getMessage())); } ?> <!DOCTYPE html> <html lang=en dir=ltr> <head> <meta charset=utf-8> <title>Authentication complete – Demonstration of the OneDrive SDK for PHP</title> <link rel=stylesheet href=//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/css/bootstrap.min.css> <link rel=stylesheet href=//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/css/bootstrap-theme.min.css> <meta name=viewport content="width=device-width, initial-scale=1"> </head> <body> <div class=container>
<?php $config = (include __DIR__ . '/../config.php') or die('Configuration file not found'); require_once __DIR__ . '/../vendor/autoload.php'; use Krizalys\Onedrive\Client; try { // Instantiates a OneDrive client bound to your OneDrive application. $onedrive = new Client(array('client_id' => $config['ONEDRIVE_CLIENT_ID'])); // Gets a log in URL with sufficient privileges from the OneDrive API. $url = $onedrive->getLogInUrl(array('wl.signin', 'wl.basic', 'wl.contacts_skydrive', 'wl.skydrive_update'), $config['ONEDRIVE_CALLBACK_URI']); session_start(); // Persist the OneDrive client' state for next API requests. $_SESSION = array('onedrive.client.state' => $onedrive->getState()); } catch (\Exception $e) { $status = sprintf('<p>Reason: <cite>%s</cite></p>', htmlspecialchars($e->getMesssage())); $url = null; } ?> <!DOCTYPE html> <html lang=en dir=ltr> <head> <meta charset=utf-8> <title>Demonstration of the OneDrive SDK for PHP</title> <link rel=stylesheet href=//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/css/bootstrap.min.css> <link rel=stylesheet href=//ajax.aspnetcdn.com/ajax/bootstrap/3.2.0/css/bootstrap-theme.min.css> <meta name=viewport content="width=device-width, initial-scale=1"> <meta name=description content="This website provides a live demonstration of the OneDrive SDK for PHP, by Krizalys. See krizalys.com for more details."> </head> <body> <div class=container> <h1>Demonstration of the OneDrive SDK for PHP</h1>