/************************************************ We are going to create both YouTube and Drive services, and query both. ************************************************/ $yt_service = new Postman_Google_Service_YouTube($client); $dr_service = new Postman_Google_Service_Drive($client); /************************************************ Boilerplate auth management - see user-example.php for details. ************************************************/ if (isset($_REQUEST['logout'])) { unset($_SESSION['access_token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); } /************************************************ If we're signed in, retrieve channels from YouTube and a list of files from Drive. ************************************************/ if ($client->getAccessToken()) { $_SESSION['access_token'] = $client->getAccessToken(); $dr_results = $dr_service->files->listFiles(array('maxResults' => 10));
************************************************/ $client_id = '<YOUR_CLIENT_ID>'; $client_secret = '<YOUR_CLIENT_SECRET>'; $redirect_uri = '<YOUR_REDIRECT_URI>'; $client = new Postman_Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); $service = new Postman_Google_Service_Drive($client); if (isset($_REQUEST['logout'])) { unset($_SESSION['upload_token ']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['upload_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { $client->setAccessToken($_SESSION['upload_token']); if ($client->isAccessTokenExpired()) { unset($_SESSION['upload_token']); } } else { $authUrl = $client->createAuthUrl(); } /************************************************ If we're signed in then lets try to upload our file. ************************************************/
public function testSettersGetters() { $client = new Postman_Google_Client(); $client->setClientId("client1"); $client->setClientSecret('client1secret'); $client->setState('1'); $client->setApprovalPrompt('force'); $client->setAccessType('offline'); $client->setRedirectUri('localhost'); $client->setApplicationName('me'); $this->assertEquals('object', gettype($client->getAuth())); $this->assertEquals('object', gettype($client->getCache())); $this->assertEquals('object', gettype($client->getIo())); $client->setAuth(new Postman_Google_Auth_Simple($client)); $client->setAuth(new Postman_Google_Auth_OAuth2($client)); try { $client->setAccessToken(null); die('Should have thrown an Postman_Google_Auth_Exception.'); } catch (Postman_Google_Auth_Exception $e) { $this->assertEquals('Could not json decode the token', $e->getMessage()); } $token = json_encode(array('access_token' => 'token')); $client->setAccessToken($token); $this->assertEquals($token, $client->getAccessToken()); }
$client->setApplicationName("Client_Library_Examples"); $service = new Postman_Google_Service_Books($client); /************************************************ If we have an access token, we can carry on. Otherwise, we'll get one with the help of an assertion credential. In other examples the list of scopes was managed by the Client, but here we have to list them manually. We also supply the service account ************************************************/ if (isset($_SESSION['service_token'])) { $client->setAccessToken($_SESSION['service_token']); } $key = file_get_contents($key_file_location); $cred = new Postman_Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/books'), $key); $client->setAssertionCredentials($cred); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $_SESSION['service_token'] = $client->getAccessToken(); /************************************************ We're just going to make the same call as in the simple query as an example. ************************************************/ $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); echo "<h3>Results Of Call:</h3>"; foreach ($results as $item) { echo $item['volumeInfo']['title'], "<br /> \n"; } echo pageFooter(__FILE__);
/************************************************ If we're logging out we just need to clear our local access token in this case ************************************************/ if (isset($_REQUEST['logout'])) { unset($_SESSION['access_token']); } /************************************************ If we have a code back from the OAuth 2.0 flow, we need to exchange that with the authenticate() function. We store the resultant access token bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } /************************************************ If we have an access token, we can make requests, else we generate an authentication URL. ************************************************/ if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); } else { $authUrl = $client->createAuthUrl(); } /************************************************ If we're signed in and have a request to shorten a URL, then we create a new URL object, set the