【问题标题】:Accessing google spreadsheet API restrictions in PHP issue在 PHP 问题中访问 google 电子表格 API 限制
【发布时间】:2021-08-06 10:38:33
【问题描述】:

我创建了一个电子表格,我成功地使用google api 库并通过从Google console -> Api & Services -> Credentials 创建service accounts 凭据成功地更新了一些数据。

我的问题是尝试限制电子表格文件的访问。如果不允许对具有编辑角色的每个人公开可用,它将不起作用。我收到这条消息:

{
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "errors": [
      {
        "message": "The caller does not have permission",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

如果我允许所有人访问我的电子表格(如下图所示),它将起作用。

我也在运行我的脚本,它通过 cronjob 更新电子表格。这也是我执行身份验证部分的代码,我正在使用来自 Service Contract 的数据:

        $client = new Google_Client();
        $client->setApplicationName('Google Sheets and PHP');
        $configuration = '{
  "type": "'.$apiConfig['keyFile']['type'].'",
  "project_id": "'.$apiConfig['projectId'].'",
  "private_key_id": "'.$apiConfig['keyFile']['private_key_id'].'",
  "private_key": "'.$key.'",
  "client_email": "'.$apiConfig['keyFile']['client_email'].'",
  "client_id": "'.$apiConfig['keyFile']['client_id'].'",
  "auth_uri": "'.$apiConfig['keyFile']['auth_uri'].'",
  "token_uri": "'.$apiConfig['keyFile']['token_uri'].'",
  "auth_provider_x509_cert_url": "'.$apiConfig['keyFile']['auth_provider_x509_cert_url'].'",
  "client_x509_cert_url": "'.$apiConfig['keyFile']['client_x509_cert_url'].'"
}';

        $client->setAuthConfig(json_decode($configuration, true));
        $client->setScopes([Google_Service_Sheets::SPREADSHEETS]);
        $client->setAccessType('offline');

        return new Google_Service_Sheets($client);

有人知道如何限制访问吗?还有这个工作吗?

非常感谢您的任何建议

【问题讨论】:

    标签: php google-sheets google-api


    【解决方案1】:

    最后我通过添加这个电子邮件地址让它工作了:

    Share with people and groups。请参阅我的帖子中的图片(问题)。

    【讨论】:

      猜你喜欢
      • 2013-12-22
      • 2017-01-23
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多