2014-10-06 52 views
-1

当使用下面的代码来尝试列出了我刚刚成立了一个全新的Gmail帐户收到的消息,我得到无法listUserMessages() - 500后端错误

[Google_Service_Exception] Error calling GET https://www.googleapis.com/gmail/v1/users/myGmailAccount%40gmail.com/messages: (500) Backend Error

我知道验证部分正常工作,因为我可以对this example中显示的内容进行小修改并查询书籍API。下面是我用来尝试查询最近收到的消息我已经授权给Gmail的访问API的代码...

const EMAIL = '[email protected]'; 

private $permissions = [ 
    'https://www.googleapis.com/auth/gmail.readonly' 
]; 

private $serviceAccount = '[email protected]'; 

/** @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); 
} 

enter image description here

500让我相信这是一个内部错误gmail API或我错过了PHP客户端未验证的内容。

回答

2

这一个为我工作。我的理解是服务帐户没有邮箱,它不太确定应该使用哪个邮箱。所以你不能使用listUsersMessages()函数来获取所有消息。

因此您需要指定服务帐户需要处理的电子邮件地址。

确保范围已允许在Web应用程序的API来

1.加入这一行:

$credentials->sub = self::EMAIL; 

前:

$client->setAssertionCredentials($credentials); 

2.然后更新你的getMessages()到:

$this->gmail->users_messages->listUsersMessages("me"); 

希望这有助于!

0

我想这个帐户已经登录到web界面了吧?你有没有尝试过一个单独的用户?如何做像标签列表的东西呢?你有没有尝试使用API​​浏览器网站? https://developers.google.com/apis-explorer/#p/gmail/v1/

您能否提供开发者客户端ID的前5位数字(这不是秘密),所以我可以尝试追查错误?

+0

我已经登录到Web界面中找到。 API资源管理器正常工作。列表标签抛出相同的错误。我会给另一个用户一个尝试....客户端ID:10391 – Webnet 2014-10-06 18:05:48

+0

我尝试使用其他用户并遇到相同的错误。 – Webnet 2014-10-06 18:16:50

+0

当您使用上面链接的APIs浏览器站点为用户时,情况如何?你能否在开发者控制台确认你的认证设置?例如在“API> Credentials”中检查oauth2,客户端类型等,并确保您在“API> Consent Screen”上设置了项目名称和电子邮件。 – 2014-10-06 20:38:37

0

为了这个工作,你必须模拟一个用户帐户(服务帐户没有邮箱,因为lthh89vt指出)。

如果您尝试直接访问邮箱,则会出现(500) Back End错误。

完整的代码是:

$client_email = '[email protected]'; 
$private_key = file_get_contents('MyProject.p12'); 
$scopes = array('https://www.googleapis.com/auth/gmail.readonly'); 

$user_to_impersonate = '[email protected]'; 
$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'); 

全部指令可以在Google APIs Client Library for PHP