Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法从google电子表格中读取_Php_Google Sheets_Google Api_Google Spreadsheet Api - Fatal编程技术网

Php 无法从google电子表格中读取

Php 无法从google电子表格中读取,php,google-sheets,google-api,google-spreadsheet-api,Php,Google Sheets,Google Api,Google Spreadsheet Api,我的目标是阅读谷歌电子表格,根据表格中的数据执行一些操作,然后更新表格 我试图访问的工作表是由其他人创建的,而我拥有读取、写入和删除的所有权限。现在,我手动执行同样的操作 我正试图通过这种方式来实现 <?php namespace Lib; class SpreadSheet { /** * @var Application name from google console */ private $application_name; /**

我的目标是阅读谷歌电子表格,根据表格中的数据执行一些操作,然后更新表格

我试图访问的工作表是由其他人创建的,而我拥有读取、写入和删除的所有权限。现在,我手动执行同样的操作

我正试图通过这种方式来实现

<?php

namespace Lib;

class SpreadSheet {
    /**
     * @var Application name from google console
     */
    private $application_name;

    /**
     * @var client id provided by google
     */
    private $client_id;

    /**
     * @var client secret
     */
    private $client_secret;

    /**
     *@var developer key
     */
    private $developer_key;
    /**
     * @var client which will be used to connect to all the services
     */
    public $client;

    public $spreadsheet_id;

    public function __construct($spreadsheet_id, $google_auth_credentials) {
        if (!is_null($google_auth_credentials) &&
            is_array($google_auth_credentials) &&
            (count($google_auth_credentials) == 4)
        ) {
            $this->spreadsheet_id = $spreadsheet_id;
            $this->setCredentials($google_auth_credentials);
        } else {
            throw new \Exception('Invalid credentials for google spreadsheet');
        }
    }

    /**
     * Make a google client.
     */
    private function createGoogleClient() {
        $this->client = new \Google_Client();
        $this->client->setApplicationName($this->application_name);
        $this->client->setClientId($this->client_id);
        $this->client->setClientSecret($this->client_secret);
        $this->client->setDeveloperKey($this->developer_key);

        return $this->client;
    }

    /**
     * Validates and sets credentials. Raises Exception if there is an issue.
     *
     * @param array $credentials Array containing Consumer Key, Secret and Access Token, Secret
     *
     * @return bool Always returns true
     */
    private function setCredentials($credentials) {
        if (!isset($credentials['application_name']) && strlen($credentials['application_name']) == 0) {
            throw new \Exception('Unable to get application_name from credentials');
        } else {
            $this->application_name = $credentials['application_name'];
        }

        if (!isset($credentials['client_id']) && strlen($credentials['client_id']) == 0) {
            throw new \Exception('Unable to get client_id from credentials');
        } else {
            $this->client_id = $credentials['client_id'];
        }

        if (!isset($credentials['client_secret']) && strlen($credentials['client_secret']) == 0) {
            throw new \Exception('Unable to get client_secret from credentials');
        } else {
            $this->client_secret = $credentials['client_secret'];
        }

        if (!isset($credentials['developer_key']) && strlen($credentials['developer_key']) == 0) {
            throw new \Exception('Unable to get developer_key from credentials');
        } else {
            $this->developer_key = $credentials['developer_key'];
        }
        $this->createGoogleClient();
        return true;
    }

    public function readSheet($range) {
        $service = new \Google_Service_Sheets($this->client);
        try {
            $response = $service->spreadsheets_values->get(
                $this->spreadsheet_id,
                $range);
            $values = $response->getValues();
            return $values;
        } catch (\Google_Service_Exception $e) {
            return $e->getMessage();
        }
    }
}
我已经从代码中删除了凭据,但它们仍然存在并且很好。我正在使用这些凭据中的其他服务。

从电子表格返回一系列值。调用方必须指定电子表格ID和范围。需要以下OAuth作用域之一:

我也不知道你为什么要创建一个新的工作表,你只需要通过工作表id和你正在寻找的范围

 $response = $service->spreadsheets_values->get($spreadsheetId, $range);

您可能想尝试阅读

您请求的范围是什么?我假设您也删除了工作表id$spreadsheet_id=“”;我没有设置任何范围。。这是我正在使用的所有代码
{
  "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"
  }
}
 $response = $service->spreadsheets_values->get($spreadsheetId, $range);