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()); } ?> 
2
  • Tried this? Commented Jan 4, 2016 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. (stackoverflow.com/questions/29341096/…) Commented Jan 5, 2016 at 14:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.