public function testRevokeAccess() { $accessToken = "ACCESS_TOKEN"; $refreshToken = "REFRESH_TOKEN"; $accessToken2 = "ACCESS_TOKEN_2"; $token = ""; $client = $this->getClient(); $response = $this->getMock("Google_Http_Request", array(), array('')); $response->expects($this->any())->method('getResponseHttpCode')->will($this->returnValue(200)); $io = $this->getMock("Google_IO_Stream", array(), array($client)); $io->expects($this->any())->method('makeRequest')->will($this->returnCallback(function ($request) use(&$token, $response) { $elements = array(); parse_str($request->getPostBody(), $elements); $token = isset($elements['token']) ? $elements['token'] : null; return $response; })); $client->setIo($io); // Test with access token. $oauth = new Google_Auth_OAuth2($client); $oauth->setAccessToken(json_encode(array('access_token' => $accessToken, 'created' => time(), 'expires_in' => '3600'))); $this->assertTrue($oauth->revokeToken()); $this->assertEquals($accessToken, $token); // Test with refresh token. $oauth = new Google_Auth_OAuth2($client); $oauth->setAccessToken(json_encode(array('access_token' => $accessToken, 'refresh_token' => $refreshToken, 'created' => time(), 'expires_in' => '3600'))); $this->assertTrue($oauth->revokeToken()); $this->assertEquals($refreshToken, $token); // Test with passed in token. $this->assertTrue($oauth->revokeToken($accessToken2)); $this->assertEquals($accessToken2, $token); }
public function testSign() { $client = $this->getClient(); $oauth = new Google_Auth_OAuth2($client); $client->setClientId('clientId1'); $client->setClientSecret('clientSecret1'); $client->setRedirectUri('http://localhost'); $client->setDeveloperKey('devKey'); $client->setAccessType('offline'); $client->setApprovalPrompt('force'); $client->setRequestVisibleActions('http://foo'); $req = new Google_Http_Request($client, 'http://localhost'); $req = $oauth->sign($req); $this->assertEquals('http://localhost?key=devKey', $req->getUrl()); // test accessToken $oauth->setAccessToken(json_encode(array('access_token' => 'ACCESS_TOKEN', 'created' => time(), 'expires_in' => '3600'))); $req = $oauth->sign($req); $auth = $req->getRequestHeader('authorization'); $this->assertEquals('Bearer ACCESS_TOKEN', $auth); }
* Implements: * - Retrieval of access tokens * - Refresh of an existing access token * - Storage of access tokens in sessions * * Requirements: * - Google APIs Client Libraries: https://developers.google.com/discovery/libraries */ $client = new Google_Client(); $oauthClient = new Google_Auth_OAuth2($client); $client->setApplicationName(SERVICE_APP_NAME); $client->setClientId(SERVICE_CLIENT_ID); $token_name = "oauth_client_" . $client_id . "_token"; $token = (array) json_decode($_SESSION[$token_name]); if (isset($token["access_token"]) && time() <= $token["created"] + $token["expires_in"]) { $oauthClient->setAccessToken($_SESSION[$token_name]); } else { $oauthClient->refreshTokenWithAssertion(new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_EMAIL, array(SCOPE), file_get_contents(KEY_FILE))); $_SESSION[$token_name] = $oauthClient->getAccessToken(); } if ($oauthClient->getAccessToken()) { // All good, we've got a valid access token! echo 'All good, let\'s go!'; // Set up some variables $asset_id = '09372590152434720789-16691395374091854705'; $token = $oauthClient->getAccessToken(); print $token; $url = 'https://www.googleapis.com/mapsengine/v1/tables/' . $asset_id . '/features?version=published&key=' . $token; print $url; $merge_on = 'features'; // Call a request