【问题标题】:Service account authentication服务帐号身份验证
【发布时间】:2014-06-18 07:21:39
【问题描述】:

我正在尝试通过 PHP 为一个特定用户创建日历事件 - 比如 development@example.com。

我在 Google Developers Console 中创建了服务帐户,获得了 ClientID、电子邮件地址和私钥。验证通过代码完成:

$client = new Google_Client();
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$cred = new Google_Auth_AssertionCredentials(
  'somelongstring@developer.gserviceaccount.com.',
  array('https://www.googleapis.com/auth/calendar','https://www.googleapis.com/auth/calendar.readonly'),
  file_get_contents('p12 file'));
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

这种类型的身份验证看起来还不错。但是所有事件都是作为用户创建的,电子邮件地址为 somelongstring@developer.gserviceaccount.com 而不是 development@example.com。

我试过设置子参数:

$cred = new Google_Auth_AssertionCredentials(
....
$cred->sub = 'developement@example.com';
$client->setAssertionCredentials($cred);

但是这段代码抛出异常:

Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ "error" : "access_denied", "error_description" : "Requested client not authorized." }'

现在我迷路了。有什么建议吗?

【问题讨论】:

    标签: google-calendar-api google-api-php-client service-accounts


    【解决方案1】:

    好的,已解决;-)

    问题在于在自己的域上进行开发。

    正如other questionGoogle SDK Guide 中所述,我必须向我请求访问权限的所有 范围授予服务帐户的访问权限。我忘了添加只读范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 2021-03-22
      • 2015-09-16
      • 1970-01-01
      • 2021-01-16
      • 2019-11-05
      相关资源
      最近更新 更多