Неверный запрос при использовании API Gmail

Я пытаюсь подключиться к Gmail, но сервер говорит:

Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/gmail/v1/users/{test}%40gmail.com/messages: (400) Bad Request' in C:\...\google-api-php-client\src\Google\Http\REST.php on line 110 

Кажется, я не могу найти проблему. Вот код:

 $google_accounts = $this->getGoogleAccounts(); if (count($google_accounts) > 0) { require_once $_SERVER['DOCUMENT_ROOT'] . '/include/google-api-php-client/src/Google/autoload.php'; require_once $_SERVER['DOCUMENT_ROOT'] . '/include/google-api-php-client/src/Google/Client.php'; require_once $_SERVER['DOCUMENT_ROOT'] . '/include/google-api-php-client/src/Google/Service/Gmail.php'; $scopes = array( 'https://mail.google.com', 'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.modify', ); $key_file_location = $_SERVER['DOCUMENT_ROOT'] . '/include/google-api-php-client/src/keys/'; foreach ($google_accounts as $one_account) { if (!empty($one_account->client_id) && !empty($one_account->service_mail) && !empty($one_account->key_file)) {var_dump($one_account); $key = file_get_contents($key_file_location . $one_account->key_file); $client = new Google_Client(); $cred = new Google_Auth_AssertionCredentials($one_account->service_mail, $scopes, $key); $client->setAssertionCredentials($cred); $client->setClientId($one_account->client_id); $client->setAccessType('offline_access'); $service = new Google_Service_Gmail($client); $opt_param = array(); $messagesResponse = $service->users_messages->listUsersMessages($one_account->login, $opt_param); var_dump($messagesResponse); } } return $list; } else { return false; } 

Вам нужно добавить:

 $cred->sub = $userEmail; 

сразу после создания $ cred, где $ userEmail – это адрес электронной почты пользователя, которого вы пытаетесь выдавать. Для справки см. Документ учетной записи службы Google Диска, который использует другую область действия и вызов API, но в остальном аналогичен тому, что вы хотите использовать с API Gmail.