Google Sheeet API的问题 [英] Issue with Google Sheeet API

查看:57
本文介绍了Google Sheeet API的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Web服务能够使用PHP写入GoogleSheet API.

I am trying to make web service to write into GoogleSheet API with PHP.

为此,我正在关注Google控制台的文档

for that, I am following the documentation of google console

链接

并编写以下代码

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

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

function getClient()
{
  $client = new Google_Client();
  $client->setApplicationName('Google Sheets API PHP Quickstart');
  $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
  $client->setAuthConfig('credentials.json');
  $client->setAccessType('offline');
  $client->setPrompt('select_account consent');

  $tokenPath = 'token.json';
  if (file_exists($tokenPath)) {
    $accessToken = json_decode(file_get_contents($tokenPath), true);
    $client->setAccessToken($accessToken);
  }

  if ($client->isAccessTokenExpired()) {
    if ($client->getRefreshToken()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    } 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);
        $client->setAccessToken($accessToken);

        if (array_key_exists('error', $accessToken)) {
            throw new Exception(join(', ', $accessToken));
        }
    }
    if (!file_exists(dirname($tokenPath))) {
        mkdir(dirname($tokenPath), 0700, true);
    }
    file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
  return $client;
}


$client = getClient();
$service = new Google_Service_Sheets($client);

$spreadsheetId = 'XXXX';
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = [["This","is","a","new","row"],];
$body = new Google_Service_Sheets_ValueRange([
  "values" =>$values
]);
$params = [
  "valueInputOption" => "RAW",
   "insertDataOption" => "INSERT_ROWS"
];
$result = $service->spreadsheets_values->append(
  $spreadsheetId,
  $range,
  $body,
  $params
);

但是当我运行这段代码时,这给了我下面给出的错误.

But when I run this code this gives me the error which is given below.

[25-Nov-2019 01:53:03美国/芝加哥] PHP致命错误:未捕获的异常"Exception",消息为此应用程序必须在命令行上运行".在Google_sheet/quickstart.php:5中 堆栈跟踪:

[25-Nov-2019 01:53:03 America/Chicago] PHP Fatal error: Uncaught exception 'Exception' with message 'This application must be run on the command line.' in Google_sheet/quickstart.php:5 Stack trace:

thrown in Google_sheet/quickstart.php on line 5

您能帮我吗

推荐答案

它说得很清楚:

此应用程序必须在命令行上运行.

This application must be run on the command line.

因此,这意味着您未使用命令行运行它,而是尝试使用浏览器访问它.如果您按照快速入门指南所述:

So, this means you are not running it using the command line, you are trying to access it using the browser. If you do as the quickstart guide says:

php quickstart.php

然后它将起作用.

为什么使用浏览器访问时会看到此异常?

由于以下行位于代码的开头:

Because of this lines placed at the beginning of your code:

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

要在浏览器上运行该怎么办?

只需删除这些行,您就可以使用浏览器运行脚本.

Just remove those lines and you'll be able to run the script using the browser.

这篇关于Google Sheeet API的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆