无法从谷歌电子表格中读取

时间:2017-03-27 09:44:30

标签: php google-sheets google-api google-spreadsheet-api

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

我尝试访问的表单是由其他人创建的,而我拥有读取,写入,删除所有权限。我现在手动做同样的事。

我试图通过这种方式实现。

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

我从另一个文件index.php

调用这个类
<?php

include dirname(__DIR__) . "/vendor/autoload.php";

use GuzzleHttp\Client;
use Lib\SpreadSheet;

$google_api_credentials = [
    "application_name" => "",
    "client_id" => "",
    "client_secret" => "",
    "developer_key" => "",
];
$spreadsheet_id = "";
$spreadsheet = new SpreadSheet($spreadsheet_id, $google_api_credentials);
$range = 'Sheet1';
$sheet_content = $spreadsheet->readSheet($range);
var_dump($sheet_content);

但每次我收到错误

{
  "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"
  }
}

我已从代码中删除了凭据,但它们存在并且很好。我正在使用这些凭证中的其他服务。

1 个答案:

答案 0 :(得分:0)

Method: spreadsheets.values.get返回电子表格中的一系列值。调用者必须指定电子表格ID和范围。需要以下OAuth范围之一:

我也不确定您为什么要创建新工作表,只需要传递工作表ID和您要查找的范围。

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

您可能想尝试阅读PHP Quickstart