使用服务帐户的GMAIL REST API - 不允许清空或缺少范围

时间:2016-01-04 21:55:11

标签: php google-oauth gmail-api service-accounts

我尝试通过 GMAIL REST API v2 并使用service account key (a solution for server to server communication)从我的GMAIL收件箱中获取最新的电子邮件。但是,我收到以下错误:

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

以下是我的PHP代码。我使用composer来安装google API PHP client

有没有人遇到并解决了这个问题?

<?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());
}
?>

0 个答案:

没有答案