3

I a trying to get the latest emails from my GMAIL inbox via the GMAIL REST API v2 and using a service account key (a solution for server to server communication). However, I get the following error:

{ "error": "invalid_scope", "error_description": "Empty or missing scope not allowed." }

Below is my PHP code. I have used composer to install the google API PHP client.

Has anyone encountered and solved this issue?

<?php

require_once 'vendor/autoload.php';
$scopes = array('https://www.googleapis.com/auth/gmail.readonly');
$client = new Google_Client();
$client->setAuthConfig('serviceaccount-xxxxxxxxx.json');

try{
  $service = new Google_Service_Gmail($client);
  $opt_param['maxResults'] = 5; // Return Only 5 Messages
  $opt_param['labelIds'] = 'INBOX'; // Only show messages in Inbox
  $messages = $service->users_messages->listUsersMessages('me',$opt_param);
  $list = $messages->getMessages();
  var_dump($list);
} catch (Exception $e) {
  print($e->getMessage());
}
?>
Loko
  • 6,539
  • 14
  • 50
  • 78
Jimmy Nsenga
  • 156
  • 2
  • 7
  • [Tried this](http://stackoverflow.com/questions/30661489/gmail-api-insufficient-permission)? – Tholle Jan 04 '16 at 22:01
  • 1
    Thanks. I realized that I forgot to set the scope $client->setScopes($scopes); However, now I get a "failedprecondition" error because I haven't impersonate the service. From the below discussion, it seems impersonation only works with a Google domain or Google Apps, which is not the case for me since I am using my personal Google Account. (http://stackoverflow.com/questions/29341096/gmail-rest-api-using-google-credentials-without-impersonate) – Jimmy Nsenga Jan 05 '16 at 14:08

0 Answers0