【问题标题】:Want to add new record/row in Google spreadsheet in PHP想要在 PHP 中的 Google 电子表格中添加新记录/行
【发布时间】:2015-05-31 05:31:31
【问题描述】:

我想通过 php 编码在 Google 电子表格中添加新记录。我搜索并找到了一个使用 gmail id 和密码系统进行身份验证的解决方案。它最初可以工作,但 2 天后它突然停止工作。代码如下:

<?php

include 'spreadsheet.php';
$Spreadsheet = new Spreadsheet("xxxxx@gmail.com", "xxxxxxx");
$Spreadsheet->setSpreadsheet("Tester")->setWorksheet("Sheet1")->add(array("First Name" => "Cell 1", "Last Name" => "Cell 2"));

?>

它停止工作后,我才知道谷歌已经改变了它的登录系统,我需要迁移到 Oauth 系统进行身份验证。

经过长时间的研发,我找到了一个例子——“https://github.com/asimlqt/php-google-spreadsheet-client”。但它不起作用,在梳理了我的各种搜索来源后,我开发了以下代码:

<?php

include_once "google-api-php-client/examples/templates/base.php";

/************************************************
  Make an API request authenticated with a service
  account.
 ************************************************/
require_once realpath(dirname(__FILE__) . '/google-api-php-client/src/Google/autoload.php');

$accessToken = getGoogleTokenFromKeyFile("XXXXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXX");

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

//ServiceRequestFactory::setInstance(new DefaultServiceRequest($accessToken));

// Load spreadsheet and worksheet
$worksheet = (new Google\Spreadsheet\SpreadsheetService())
    ->getSpreadsheets()
    ->getByTitle('Sheet1')       // Spreadsheet name
    ->getWorksheets()
    ->getByTitle('Tester');      // Worksheet name
$listFeed = $worksheet->getListFeed();

// Uncomment this to find out what Google calls your column names
// print_r($listFeed->getEntries()[0]->getValues());

// Add a new blank row to the spreadsheet, using the column headings
$listFeed->insert(['name' => 'Simon', 'age' => 25, 'gender' => 'male']);

/**
 * Retrieves a Google API access token by using a P12 key file,
 * client ID and email address
 *
 * These three things may be obtained from 
 * https://console.developers.google.com/
 * by creating a new "Service account"
 */
function getGoogleTokenFromKeyFile($clientId, $clientEmail, $pathToP12File) {
    $client = new Google_Client();
    $client->setClientId($clientId);

    $cred = new Google_Auth_AssertionCredentials(
        $clientEmail,
        array('https://spreadsheets.google.com/feeds'),
        file_get_contents($pathToP12File)
    );

    $client->setAssertionCredentials($cred);

    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }

    $service_token = json_decode($client->getAccessToken());
    return $service_token->access_token;
}


?>

但不幸的是,这个也无法正常工作,并且在突然的时间范围后,它在我的本地 xampp 服务器中显示请求超时错误。

到目前为止,我的申请一直处于搁置状态。现在真的不知道该怎么办。如果有人对此有任何具体的解决方案,请与我分享。我的主要目的是当用户在我的网站上提交他的详细信息时将数据添加到谷歌电子表格。或者如果在谷歌更改身份验证系统后无法实现,请也确认我。我想找到这个问题的最终解决方案。

提前致谢。

【问题讨论】:

标签: php google-sheets google-spreadsheet-api


【解决方案1】:

如果您能够检索到有效令牌,则在请求标头中使用“Bearer yourtokenstring”传递令牌。谷歌最近从“GoogleLogin auth=yourtokenstring”更改了这个。请改用 Bearer 标签。

如果您正在努力检索有效的访问令牌,请考虑使用带有 p12 密钥文件的服务帐户。将电子表格的编辑权限授予服务帐户电子邮件地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2016-10-27
    相关资源
    最近更新 更多