通过服务帐户访问文件时出错404

时间:2017-08-29 09:18:48

标签: php excel google-api google-oauth

我必须在google驱动器上阅读excel文件。我需要自动连接到谷歌驱动器,而不提示任何形式...机器人必须阅读文件...我有这个代码:

$service_account_file = dirname(__FILE__) . '/key-85ac95ab1e62.json';
$spreadsheet_id = 'the_id_inside_the_url_of_the_file';

$spreadsheet_range = 'Sheet1!A1:A1';
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);
$result = $service->spreadsheets_values->get($spreadsheet_id, $spreadsheet_range);
var_dump($result->getValues());

出了点问题......我解释了我是如何生成key-*.json文件的:

Google Console Developer我点击了服务帐户,然后我创建了一个新的并下载了json ..这个json是key-*.json文件。我在我的项目中添加了这个文件(正如你在第一行看到的那样)...在这里我复制了Account Service Id我将在以后使用...

现在我解释一下我如何获得spreadheet_id ...我继续使用我的谷歌驱动器,右键单击该文件,然后我与Account Service Id共享(之前复制过)并且已经有了网址生成...如果我复制并浏览浏览器中的url一切正常但项目中没有...我从url中获取spreadsheet_id

这是我收到的错误:

PHP Fatal error:  Uncaught Google_Service_Exception: {
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "errors": [
      {
        "message": "Requested entity was not found.",
        "domain": "global",
        "reason": "notFound
      }
    ],
    "status": "NOT_FOUND
  }
}

怎么了?该文件存在,我可以在浏览器中看到它使用我在php文件中使用的相同的URL ...所以我认为这是一个连接或登录问题......

任何想法?

1 个答案:

答案 0 :(得分:0)

这是服务帐户的常见问题。请参阅其中任何https://stackoverflow.com/search?q=404+service+account

简单来说,服务帐户与您用来创建帐户的Google帐户相同。要么不使用服务帐户,要么将文件从自己的帐户共享到服务帐户,以便访问它。

相关问题