【问题标题】:Append to the google spreadsheet using php使用 php 附加到 google 电子表格
【发布时间】:2018-07-23 16:40:26
【问题描述】:
<?php 
   require_once __DIR__ . '/vendor/autoload.php';
   date_default_timezone_set('America/Los_Angeles');

   define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
   define('CREDENTIALS_PATH', 'token.json');
   define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
   define('SHEET_ID', '1Ygd6cEvi0tcGa3GF11hUIZfomh0Ms9r1mZN-MfcqruE');
   define('SCOPES', implode(' ', array(
    Google_Service_Sheets::SPREADSHEETS)
   ));

   function getClient() {
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfig(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
      $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } else {
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    if(!file_exists(dirname($credentialsPath))) {
     mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);
  }
  $client->setAccessToken($accessToken);
  if ($client->isAccessTokenExpired()) {
    $refreshToken = $client->getRefreshToken();
    $client->refreshToken($refreshToken);
    $newAccessToken = $client->getAccessToken();
    $newAccessToken['refresh_token'] = $refreshToken;
    file_put_contents($credentialsPath, json_encode($newAccessToken));
  }
  return $client;
 }

 function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
   $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
 }
 $client = getClient();
 $service = new Google_Service_Sheets($client);
 $spreadsheetId = SHEET_ID;
 $range = 'Sales 3!A5:B';

 $values = array(
   array('Appended', 'Row')
 );
 $body = new Google_Service_Sheets_ValueRange(array(
  'values' => $values
 ));
 $params = array(
  'valueInputOption' => "RAW"
 );
 $result = $service->spreadsheets_values->append($spreadsheetId, $range,
  $body, $params);

我正在检索权限错误:

致命错误:未捕获的 Google_Service_Exception:{“错误”:{“代码”:403,“消息”:“请求的身份验证范围不足。”,“错误”:[{“消息”:“请求有身份验证范围不足。”、“域”:“全局”、“原因”:“禁止”} ]、“状态”:“PERMISSION_DENIED”} } 在 C:\xampp\htdocs\dashboard-redmine.org\vendor\google \apiclient\src\Google\Http\REST.php:118 堆栈跟踪:#0 C:\xampp\htdocs\dashboard-redmine.org\vendor\google\apiclient\src\Google\Http\REST.php(94) : Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 C:\xampp\htdocs\dashboard-redmine.org\vendor\google\ apiclient\src\Google\Task\Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\ htdocs\dashboard-redmine.org\vendor\google\apiclient\src\Google\Http\REST.php(58): Google_Task_Runner->run() #3 C:\xampp\htdocs\dashboa 在 C:\xampp\htdo cs\dashboard-redmine.org\vendor\google\apiclient\src\Google\Http\REST.php 在第 118 行

但是当我使用 get 方法时它工作正常 在面板中它也正常工作:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append?apix=true

【问题讨论】:

    标签: php google-sheets-api


    【解决方案1】:

    为了更好地指导您,请检查您已完成和尚未完成的过程。

    首先要做的是,将工作表 API 启用到 developer console。然后,您可以在documentation 中查看授权范围和过程,如果您认为您在此过程中遗漏了什么。

    最后,尝试从/Users/yourUserName/.credentials/sheets.googleapis.com-projectName/*撤销用户之前的保存并重新执行以获取新凭据。

    【讨论】:

    • 不幸的是它没有帮助
    猜你喜欢
    • 2014-09-11
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    相关资源
    最近更新 更多