Quantcast
Channel: Authenticate using Google javascript API - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Authenticate using Google javascript API

$
0
0

I am trying to use the Gmail API to access the email in a web application. I have tried the example at https://developers.google.com/gmail/api/quickstart/php and is working fine. Now I want to get the access token using javascript API and use in the above example.

gapi.auth2.authorize({  client_id: 'CLIENT_ID.apps.googleusercontent.com',  scope: 'email profile openid',  response_type: 'id_token permission'}, function(response) {  if (response.error) {    // An error happened!    return;  }  // The user authorized the application for the scopes requested.  var accessToken = response.access_token;  var idToken = response.id_token;  // You can also now use gapi.client to perform authenticated requests.});

When I manually add the response from javascript API in the php script it is showing error

Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in D:\wamp\www\gmailexample\google-api-php-client-2.2.0\src\Google\Client.php:267

Below is the php script I am using.

<?phprequire_once '/google-api-php-client-2.2.0/vendor/autoload.php';define('APPLICATION_NAME', 'Gmail API PHP Quickstart');define('CREDENTIALS_PATH', '~/.credentials/gmail-php-quickstart.json');define('CLIENT_SECRET_PATH', 'client_secret.json');// If modifying these scopes, delete your previously saved credentials// at ~/.credentials/gmail-php-quickstart.jsondefine('SCOPES', implode('', array(  Google_Service_Gmail::GMAIL_READONLY)));if (php_sapi_name() != 'cli') {  throw new Exception('This application must be run on the command line.');}/** * Returns an authorized API client. * @return Google_Client the authorized client object */function getClient() {  $client = new Google_Client();  $client->setApplicationName(APPLICATION_NAME);  $client->setScopes(SCOPES);  $client->setAuthConfig(CLIENT_SECRET_PATH);  $client->setAccessType('offline');  // Load previously authorized credentials from a file.  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);  $accessToken= json_decode('{"access_token":"asdfsasdfsasdfsasdfs","expires_in":2255,"expires_at":254877}', true);  $client->setAccessToken($accessToken);  // Refresh the token if it's expired.  if ($client->isAccessTokenExpired()) {    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));  }  return $client;}/** * Expands the home directory alias '~' to the full path. * @param string $path the path to expand. * @return string the expanded path. */function expandHomeDirectory($path) {  $homeDirectory = getenv('HOME');  if (empty($homeDirectory)) {    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');  }  return str_replace('~', realpath($homeDirectory), $path);}// Get the API client and construct the service object.$client = getClient();$service = new Google_Service_Gmail($client);// Print the labels in the user's account.$user = 'me';$results = $service->users_labels->listUsersLabels($user);if (count($results->getLabels()) == 0) {  print "No labels found.\n";} else {  print "Labels:\n";  foreach ($results->getLabels() as $label) {    printf("- %s\n", $label->getName());  }}

Please help.


Viewing all articles
Browse latest Browse all 2

Trending Articles