Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
google sheets api v4 PHP快速入门写入单个范围_Php_Google Sheets_Google Sheets Api - Fatal编程技术网

google sheets api v4 PHP快速入门写入单个范围

google sheets api v4 PHP快速入门写入单个范围,php,google-sheets,google-sheets-api,Php,Google Sheets,Google Sheets Api,我可以使用本教程阅读我的Google Sheet文档: quickstart.php: <?php require_once __DIR__ . '/vendor/autoload.php'; define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart'); define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json

我可以使用本教程阅读我的Google Sheet文档:

quickstart.php:

 <?php
require_once __DIR__ . '/vendor/autoload.php';

define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Sheets::SPREADSHEETS_READONLY)
));

if (php_sapi_name() != 'cli') {
  throw new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
  $client = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfig(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk.
    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);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }
  return $client;
}

/**
 * Expands the home directory alias '~' to the full path.
 * @param string $path the path to expand.
 * @return string the expanded path.
 */
function expandHomeDirectory($path) {
  $homeDirectory = getenv('HOME');
  if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  }
  return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);

// Prints the names and majors of students in a sample spreadsheet:

$spreadsheetId = 'myfileid';
$range = 'Class Data!A2';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();

if (count($values) == 0) {
  print "No data found.\n";
} else {
  print "Name, Major:\n";
  foreach ($values as $row) {
    // Print columns A and E, which correspond to indices 0 and 4.
    printf("%s, %s\n", $row[0], $row[4]);
  }
}
弗罗姆:

我有: PHP致命错误:

Uncaught exception 'Google_Service_Exception' with message '{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "errors": [
      {
        "message": "Request had insufficient authentication scopes.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

我的谷歌工作表文档具有修订/修改权限。我需要在一个范围内书写。这个错误是什么意思?请帮助我修改quickstart.php。

将范围更改为
Google\u Service\u Sheets::SPREADSHEETS

也试试这个

$params = array(
  'valueInputOption' => $valueInputOption
);


看看这个。尝试将
SheetsScopes.SPREADSHEETS.READONLY更改为
SheetsScopes.SPREADSHEETS
。这是请求中提供的OAuth 2.0令牌中的一个错误,该令牌不足以访问请求的数据。通过检查此项,确保使用正确且所有必要的范围。希望这有帮助!你好,艾比丽塔。谢谢你的帮助。我制作:表格。电子表格。但这没有帮助。真奇怪。php quickstart.php名称,主要错误:MY DATA(A2)php致命错误:未捕获异常“Google_Service_exception”,消息“{”error:{”code:403,“message:“请求的身份验证范围不足。”,“errors:[{”message:“请求的身份验证范围不足。”,“domain:“global”,“原因”:“禁止”}],“状态”:“权限被拒绝”}否否。更好)我删除了。凭据。新错误:“状态”:“无效的参数”。
$params = array(
  'valueInputOption' => $valueInputOption
);
 $params = array(
      'valueInputOption' => 'USER_ENTERED'
    );