Php 无法listUserMessages()-500后端错误
当使用下面的代码尝试列出我刚刚建立的全新gmail帐户收到的邮件时,我得到Php 无法listUserMessages()-500后端错误,php,google-api-php-client,gmail-api,Php,Google Api Php Client,Gmail Api,当使用下面的代码尝试列出我刚刚建立的全新gmail帐户收到的邮件时,我得到 [Google\u服务\u异常]调用GET时出错https://www.googleapis.com/gmail/v1/users/myGmailAccount%40gmail.com/messages: (500)后端错误 我知道验证部分工作正常,因为我能够进行微小的修改并查询books api,如中所示。下面是我用来尝试查询最近收到的邮件的代码 const EMAIL = 'myGmailAccount@gmail.
[Google\u服务\u异常]调用GET时出错https://www.googleapis.com/gmail/v1/users/myGmailAccount%40gmail.com/messages: (500)后端错误
我知道验证部分工作正常,因为我能够进行微小的修改并查询books api,如中所示。下面是我用来尝试查询最近收到的邮件的代码
const EMAIL = 'myGmailAccount@gmail.com';
private $permissions = [
'https://www.googleapis.com/auth/gmail.readonly'
];
private $serviceAccount = 'xxxxxxxxxxxxxxxxx@developer.gserviceaccount.com';
/** @var Google_Service_Gmail */
private $gmail = null;
/** @var null|string */
private static $serviceToken = null;
public function __construct()
{
$client = new Google_Client();
$client->setApplicationName($this->applicationName);
$this->gmail = new Google_Service_Gmail($client);
//authentication
if (isset(self::$serviceToken)) {
$client->setAccessToken(self::$serviceToken);
}
$credentials = new Google_Auth_AssertionCredentials(
$this->serviceAccount,
$this->permissions,
$this->getKey()
);
$client->setAssertionCredentials($credentials);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credentials);
}
self::$serviceToken = $client->getAccessToken();
}
public function getMessages()
{
$this->gmail->users_messages->listUsersMessages(self::EMAIL);
}
我已授权API访问gmail:
500让我相信这是gmail API的内部错误,或者我丢失了PHP客户端无法验证的内容。我想该帐户已经登录到web界面了,对吗?你有没有试过单独的用户?不如改用标签列表之类的方法?您是否尝试过使用API资源管理器站点
你能提供开发人员客户ID的前5位数字吗(反正这不是秘密),这样我就可以试着追踪错误了?这一个对我有用。我的理解是,服务帐户没有邮箱,它也不太确定应该使用哪个邮箱。因此,不能使用listUsersMessages()函数获取所有消息 因此,您需要指定服务帐户需要使用的电子邮件地址 确保Web应用程序API上已允许该作用域 1。添加此行:
$credentials->sub = self::EMAIL;
$this->gmail->users_messages->listUsersMessages("me");
之前:
$client->setAssertionCredentials($credentials);
2。然后将getMessages()更新为:
$credentials->sub = self::EMAIL;
$this->gmail->users_messages->listUsersMessages("me");
希望这有帮助 要使其工作,您必须模拟用户帐户(正如lth89vt指出的,服务帐户没有邮箱) 如果您试图直接访问邮箱,则会出现
(500)后端
错误
完整代码为:
$client_email = '1234567890-a1b2c3d4e5f6g7h8i@developer.gserviceaccount.com';
$private_key = file_get_contents('MyProject.p12');
$scopes = array('https://www.googleapis.com/auth/gmail.readonly');
$user_to_impersonate = 'user@example.org';
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate,
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Gmail($client);
$results = $service->users_messages->listUsersMessages('me');
有关详细说明,请参见我已登录到web界面。API资源管理器工作正常。列出标签会引发相同的错误。我将给另一个用户一次尝试。。。。客户端ID:10391我尝试使用其他用户,但遇到了相同的错误。当您为该用户使用上面链接的API资源管理器站点时,情况如何?您能在开发者控制台中确认您的身份验证设置吗?e、 g.在“API>凭据”上检查oauth2、客户端类型等,并确保在“API>同意屏幕”上设置了项目名称和电子邮件。您可以为同一用户登录Gmail web界面,并为同一用户发出API explorer请求吗?根据我在日志中看到的错误,似乎不太可能工作…感谢您的帮助。是的,我能够登录到web ui(当前正在查看我的收件箱),并使用成功地进行API调用。我在同意屏幕上添加了项目名称和电子邮件。已验证oAuth凭据。我仍然看到错误。