Jamie Kudla
Jamie Kudla

Reputation: 872

Error Saving Form Data to Google Spreadsheets Using PHP and the Google Docs API

I need to post the results from an html form/survey to a google docs spreadsheet. Right now I'm trying the simplest scenario with the api, which is just writing a simple row to my spreadsheet, although I keep getting an error. (I'm not too great with php, so please bear with me!)

Code:

<?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>

Error:

> 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

Any help is appreciated! :]

Upvotes: 1

Views: 1140

Answers (2)

Josh
Josh

Reputation: 3442

Your error message refers to an invalid token which means that Google is not accepting your login. I think the error is in the $service line. Try this:

$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);

Then all you have to do is this:

$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);

Upvotes: 0

Liv
Liv

Reputation: 270

It's been a while since I've worked with this, but I believe the specific error message you're seeing has to do with the service name you're using. 'xapi' is a generic service name for Google services, but you're trying to access Google Spreadsheets specifically.

Try changing:

$service = 'xapi';

To:

$service = 'wise';

Since 'wise' is the correct ClientLogin service name for Google spreadsheets.

Upvotes: 1

Related Questions