【问题标题】:Unable to listUserMessages() - 500 Backend Error无法列出UserMessages() - 500 后端错误
【发布时间】:2014-10-06 17:15:05
【问题描述】:

当使用以下代码尝试列出我刚刚设置的全新 gmail 帐户收到的邮件时,我得到了

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

我知道验证部分工作正常,因为我可以进行细微的修改并查询书籍 api,如 this example 所示。下面是我用来尝试查询最近收到的消息的代码...

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

我已授予对 gmail 的 API 访问权限:

500 让我相信这是 gmail API 的内部错误,或者我遗漏了 PHP 客户端未验证的内容。

【问题讨论】:

    标签: php google-api-php-client gmail-api


    【解决方案1】:

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

    因此,您需要指定服务帐户需要使用的电子邮件地址。

    确保 Web App API 上的范围已被允许

    1.添加这一行:

    $credentials->sub = self::EMAIL;
    

    之前:

    $client->setAssertionCredentials($credentials);
    

    2。然后将您的 getMessages() 更新为:

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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我想该帐户已经登录到网络界面了,对吧?您是否尝试过使用单独的用户?不如做一些类似标签列表的事情?您是否尝试过使用 API 资源管理器站点? https://developers.google.com/apis-explorer/#p/gmail/v1/

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

      【讨论】:

      • 我已经登录了网页界面。 API 资源管理器正常工作。列出标签会引发相同的错误。我给其他用户试试.... 客户 ID:10391
      • 我尝试使用其他用户并遇到相同的错误。
      • 当您为用户使用上面链接的 API 资源管理器站点时会怎样?您可以在开发人员控制台中确认您的身份验证设置吗?例如检查“API > 凭据”上的 oauth2、客户端类型等,并确保您在“API > 同意屏幕”上设置了项目名称和电子邮件。
      • 您可以为同一用户登录 Gmail 网络界面并为同一用户发出 API Explorer 请求吗?根据我在日志中看到的似乎不太可能正常工作的错误...
      • 感谢您的帮助。是的,我能够登录到 web ui(目前正在查看我的收件箱)并使用 developers.google.com/apis-explorer/#p/gmail/v1/… 成功进行 API 调用。我在同意屏幕上添加了项目名称和电子邮件。 oAuth 凭据已经过验证。我仍然看到错误。
      【解决方案3】:

      为此,您必须模拟一个用户帐户(服务帐户没有邮箱,正如 lthh89vt 指出的那样)。

      如果您尝试直接访问邮箱,您将收到(500) Back End 错误。

      完整代码为:

      $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');
      

      完整的说明可以在Google APIs Client Library for PHP找到

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-20
        • 2014-11-12
        • 2016-07-09
        相关资源
        最近更新 更多