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

Posted

技术标签:

【中文标题】无法从谷歌电子表格中读取【英文标题】:Unable to Read from google spreadsheet 【发布时间】:2017-03-27 09:44:29 【问题描述】:

我的目标是阅读 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"
  

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

【问题讨论】:

您要求什么范围?我假设您还删除了工作表 id $spreadsheet_id = ""; 我没有设置任何范围..这是我正在使用的所有代码 【参考方案1】:

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

https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/spreadsheets.readonly

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

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

您可能想尝试阅读PHP Quickstart

【讨论】:

以上是关于无法从谷歌电子表格中读取的主要内容,如果未能解决你的问题,请参考以下文章

有啥方法可以将数据从谷歌电子表格传递到 clickhouse?

如何从谷歌电子表格中获取 json 数据

通过在云中运行的笔记本从谷歌驱动器访问电子表格文件

用于将数据从谷歌电子表格加载到 bigquery 的独立脚本

如何从谷歌电子表格中动态变化的单元格中保存最小值和最大值?

javascript 一个应用程序脚本示例,用于从谷歌电子表格中获取大查询数据。更多信息:http://wp.me/pB1lQ-19i