首页 > 解决方案 > 在没有作曲家的情况下使用 Google 电子表格 API:如何使其工作?

问题描述

我在我的 FTP 服务器上使用 Google 电子表格 API 时遇到了 3 天的问题。OVH serv 不允许安装 Composer,所以我试图在没有这个工具的情况下安装它。我尝试了几件事,但到目前为止,它们都不能正常工作:

    function google_api_php_client_autoload($className){
  $classPath = explode('_', $className);
  if ($classPath[0] != 'Google') {
    return;
  }
  // Drop 'Google', and maximum class file path depth in this project is 3.
  $classPath = array_slice($classPath, 1, 2);
  $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php';
  if (file_exists($filePath)) {
    require_once($filePath);
  }
}
spl_autoload_register('google_api_php_client_autoload');
?>
<?php

define('DOCROOT', dirname(__FILE__));

use Spreadsheet\DefaultServiceRequest;
use Spreadsheet\ServiceRequestFactory;

if(!function_exists('classAutoLoader')){
    function classAutoLoader($class_name) {
        $class_name = str_replace('\\','/',$class_name);
        require_once(DOCROOT."/src/".$class_name.".php");
    }
}

spl_autoload_register('classAutoLoader');
require __DIR__ . '/vendor/autoload.php';
    $client = new Google_Client();
    $client->setApplicationName('Google Sheets API PHP');
    $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
    $client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $service = new Google_Service_Sheets($client);
    $spreadsheetId = "1LLpSUVBAA3dSiyOG4lKDHDxqeldazzdads";

文件 token.json 的内容:

{
  "type": "service_account",
  "project_id": "ambient-airlock-*****",
  "private_key_id": "*****************",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIE*****************==\n-----END PRIVATE KEY-----\n",
  "client_email": "*****************@ambient-airlock-*****************.iam.gserviceaccount.com",
  "client_id": "*****************",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/*****************%40ambient-airlock-***********.iam.gserviceaccount.com"
}

我确信我在使用这些的方式上遗漏了一些东西,但如果有人能向我解释如何在没有 Composer 的情况下使其正常工作,我将不胜感激。

标签: phpgoogle-sheetsgoogle-apigoogle-api-php-clientautoload

解决方案


推荐阅读