1

我需要将 html 表单/调查的结果发布到 google docs 电子表格。现在我正在尝试使用 api 的最简单方案,它只是在我的电子表格中写入一个简单的行,尽管我不断收到错误消息。(我对php不太擅长,所以请多多包涵!)

代码:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . "$_SERVER[DOCUMENT_ROOT]/ZendGdata-1.12.1/library"); 
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$user = "######";
$pass = "######";
$service = "xapi"; 

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');

$spreadsheetService = new Zend_Gdata_Spreadsheets($client);

$feed = $spreadsheetService->getSpreadsheetFeed();

foreach ($feed as $entry) {
echo 'Title: ' . $entry->title . ' - ';
echo 'Id: ' . $entry->id . '<br />';
}
$rowData = array('wood' => 'oak');

$spreadsheetKey = '######';
$worksheetId = 'od6';

try{
$insertedListEntry = $spreadsheetService->insertRow($rowData,
                                                    $spreadsheetKey,
                                                    $worksheetId);
}
catch(Zend_Gdata_App_HttpException $exception) {  
    echo "Error: " . $exception->getResponse()->getRawBody();  
} 
?>
</body>
</html>

错误:

> Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with
> message 'Expected response code 200, got 401 <HTML> <HEAD>
> <TITLE>Token invalid</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"
> TEXT="#000000"> <H1>Token invalid</H1> <H2>Error 401</H2> </BODY>
> </HTML> ' in
> C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata\App.php:714 Stack
> trace: #0
> C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata.php(219):
> Zend_Gdata_App->performHttpRequest('GET', 'https://spreads...', Array,
> NULL, NULL, NULL) #1
> C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata\App.php(880):
> Zend_Gdata->performHttpRequest('GET', 'https://spreads...', Array) #2
> C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata\App.php(768):
> Zend_Gdata_App->get('https://spreads...', NULL) #3
> C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata\App.php(210):
> Zend_Gdata_App->importUrl('https://spreads...', 'Zend_Gdata_Spre...',
> NULL) #4 C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata.php(162):
> Zend_Gdata_App->getFeed('https://spreads...', 'Zend_Gdata_Spre...') #5
> C:\xa in C:\xampp\htdocs\ZendGdata-1.12.1\library\Zend\Gdata\App.php
> on line 714

任何帮助表示赞赏!:]

4

2 回答 2

1

自从我处理这个问题以来已经有一段时间了,但我相信您看到的特定错误消息与您正在使用的服务名称有关。'xapi'是 Google 服务的通用服务名称,但您正在尝试专门访问 Google 电子表格。

尝试改变:

$service = 'xapi';

到:

$service = 'wise';

因为'wise'是 Google 电子表格的正确ClientLogin 服务名称

于 2013-02-28T15:36:21.303 回答
0

您的错误消息是指无效的令牌,这意味着 Google 不接受您的登录。我认为错误就在这$service条线上。试试这个:

$user = //user name @ gmail here
$pass = //password here
$sheet = //spreadsheet id/key here
$worksheet = "od6";

require_once('Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Http_Client');

$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);

那么你所要做的就是:

$rowData = array("column1"=>"blah", "column2"=>"blah"); //replace column1 and column2 with your actual column names and replace blah with the information you want to add
$insertedListEntry = $spreadsheetService->insertRow($rowData,
                                                    $spreadsheetKey,
                                                    $worksheetId);
于 2013-03-02T16:32:33.143 回答