0

私の目標は、Googleスプレッドシートを読み込んで、シートにあるデータに基づいていくつかのアクションを実行し、シートを更新することです。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(); 
     } 
    } 
} 

私は

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

はindex.php別のファイルから、このクラスを呼び出すことが、私はエラー

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

を取得するたびに、私はコードから認証情報を削除したが、それらが存在しています良いです。私はそれらの資格情報から他のサービスを使用しています。

+0

を読んしようとする場合がありますか?私はシートID $ spreadsheet_id = ""も削除したと仮定しています。 – DaImTo

+0

私はスコープを設定していない..これは私が使用しているすべてのコード –

答えて

0

Method: spreadsheets.values.getスプレッドシートから値の範囲を返します。呼び出し元はスプレッドシートIDと範囲を指定する必要があります。次のOAuthスコープのいずれかが必要です。

あなたはあなただけ渡す必要が新しいシートを作成している理由は私もわかりませんシートIDとあなたが探している範囲。

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

あなたはどのようなスコープを要求しているPHP Quickstart

関連する問題