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); }
try { // If we don't have a code in the query string (meaning that the user did // 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>