2011-04-18 112 views
2

我完全失去了如何以编程方式发布Google文档(特别是电子表格)。谷歌文档列表API - 如何发布文档

我读过谷歌文档列表API协议指南,并发现这一点:

http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#GettingRevisions

文章的下一节“通过发布一个版本发布的文件”开头,这是哪里我发现这个例子:

PUT /feeds/default/private/full/resource_id/revisions/revision_number 
GData-Version: 3.0 
Authorization: <your authorization header here> 
Content-Length: 722 
Content-Type: application/atom+xml 

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd='http://schemas.google.com/g/2005' 
     xmlns:docs="http://schemas.google.com/docs/2007" gd:etag="W/"DkIBR3st7ImA9WxNbF0o.""> 
    <id>https://docs.google.com/feeds/id/resource_id/revisions/1</id> 
    <updated>2009-08-17T04:22:10.440Z</updated> 
    <app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-06T03:25:07.799Z</app:edited> 
    <title>Revision 1</title> 
    <content type="text/html" src="https://docs.google.com/feeds/download/documents/Export?docId=doc_id&amp;revision=1"/> 
    <link rel="alternate" type="text/html" 
     href="https://docs.google.com/Doc?id=doc_id&amp;revision=1"/> 
    <link rel="self" type="application/atom+xml" 
     href="https://docs.google.com/feeds/default/private/full/resource_id/revisions/1"/> 
    <author> 
    <name>user</name> 
    <email>[email protected]</email> 
    </author> 
    <docs:publish value="true"/> 
    <docs:publishAuto value="false"/> 
</entry> 

我一直在检索文档列表订阅源和CRUDing工作表,但我不能让出版工作我也不知道它是如何工作的。我建立我的饲料的连接,并准备付诸数据基本设置如下:

<?php 
set_include_path($_SERVER['DOCUMENT_ROOT'].'/library/'); 

require_once 'Zend/Loader/Autoloader.php'; 
$autoloader = Zend_Loader_Autoloader::getInstance(); 
$autoloader->setFallbackAutoloader(true); 


$theId = 'my-worksheet-id'; 

$user = "my-gmail-account-name"; 
$pass = "my-gmail-account-password"; 
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME; 
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service); 

$service = new Zend_Gdata($client); 


$xml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' 
     xmlns:docs='http://schemas.google.com/docs/2007' gd:etag='W/\"DkIBR3st7ImA9WxNbF0o.\"'> 

     <id>https://docs.google.com/feeds/id/spreadsheet:$theId/revisions/1</id> 
     <updated>2009-08-17T04:22:10.440Z</updated> 
     <app:edited xmlns:app='http://www.w3.org/2007/app'>2009-08-06T03:25:07.799Z</app:edited> 
     <title>Revision 1</title> 
     <content type='text/html' src='https://docs.google.com/feeds/download/documents/Export?docId=$theId&amp;revision=1'/> 
     <link rel='alternate' type='text/html' 
      href='https://docs.google.com/Doc?id=$theId&amp;revision=1'/> 
     <link rel='self' type='application/atom+xml' 
      href='https://docs.google.com/feeds/default/private/full/spreadsheet:$theId/revisions/1'/> 
     <author> 
      <name>$user</name> 
      <email>$user</email> 
     </author> 
     <docs:publish value='true'/> 
     <docs:publishAuto value='false'/> 
     </entry>"; 

$putURL = "http://docs.google.com/feeds/default/private/full/spreadsheet:".$theId."/revisions/0"; 
$data = $service->put($xml, $putURL); 
?> 

这会导致

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 Invalid request URI 

有人能帮助我吗?有没有人以编程方式成功发布Google文档?

回答

2

假设该文件已经被创建并有XXXX

的文件ID,你需要做的是发送“PUT” REQ具有特定标题的uest,以及作为正文的XML(描述文档的条目)到特定的URL。

既然你不改变文档(仅元数据),你的目标网址看起来会像这样的任何内容...

https://docs.google.com/feeds/default/private/full/XXXX/revisions/0

你需要做的第一件事是验证使用适当的Google服务。

$client = Zend_Gdata_ClientLogin::getHttpClient(GDOC_LOGIN, GDOC_PASS,'writely'); 

使用返回的对象来获取您的身份验证令牌。

$auth_token = $client->getClientLoginToken(); 

在Zend/Gdata/App.php中是一个辅助函数,用于执行PUT请求。 准备参数,像这样这个方法...

$method = "PUT"; 
$url ="https://docs.google.com/feeds/default/private/full/XXXX/revisions/0"; 
$headers['GData-Version'] = '3.0'; 
$headers['If-Match'] = '*'; 
$headers['Authorization'] = "GoogleLogin auth = $auth_token"; 
$headers['Content-Length'] = '380'; 
$headers['Content-Type'] = 'application/atom+xml'; 
$body = <<<XML 
<?xml version='1.0' encoding='UTF-8'?> 
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007" 
    xmlns:gd="http://schemas.google.com/g/2005"> 
    <category scheme="http://schemas.google.com/g/2005#kind" 
     term="http://schemas.google.com/docs/2007#spreadsheet"/> 
    <docs:publish value="true"/> 
    <docs:publishAuto value="true"/> 
</entry> 
XML; 
$contentType = "application/atom+xml"; 
$remainingRedirects = 99; 

然后调用辅助函数...

$app = new Zend_Gdata_App(); 
$app->performHttpRequest($method, $url, $headers, $body, $contentType, $remainingRedirects); 

祝你好运! 让我知道这是否有帮助!

+0

谢谢!这使我在那里获得了99%的胜利。我收到文档ID错误,但文档正在发布。 – 2011-07-22 16:02:01

0

我是Zend_Gdata的新人,但已成功上传到Google文档。

我不知道这是否是你所追求的,但这里是我的代码:

$client = Zend_Gdata_ClientLogin::getHttpClient(
    '[email protected]', 
    'MyPassword', 
    Zend_Gdata_Docs::AUTH_SERVICE_NAME 
); 
$gdClient = new Zend_Gdata_Docs($client); 

$newDocumentEntry = $gdClient->uploadFile(
    $file, 
    null, 
    null, 
    Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI 
); 

我希望这有助于

加里

+0

他想发布一个现有的谷歌文档没有上传一个新的。 ^^ – Tivie 2011-06-21 02:14:19

0

谷歌表示,推杆的数据是错误的并回复您的代码400.

尝试放置此代码

<?xml version='1.0' encoding='UTF-8'?> 

<entry xmlns='http://www.w3.org/2005/Atom'... 
1

好之前......我从哪里开始?

首先,您的网址不正确。 (the resource ID you're using is for JSON/XML not URL)

你有

$putURL = "http://docs.google.com/feeds/default/private/full/spreadsheet:".$theId."/revisions/0"; 

,你应该有

$putURL = "http://docs.google.com/feeds/default/private/full/$theId/revisions/0"; 

(可以省略。为串联,如果您使用“作为分隔符)

现在有,因为你的其他问题重新手动创建一个xml条目。

  1. 您的授权标题丢失。
  2. 在您的XML中,您使用的是版本1,但在您的URL中,您有版本/ 0
  3. 值是手动编写的,我敢肯定您并未尝试发布2年前的文件。和
  4. 必须匹配检索的etag,否则您将无法执行任何PUT请求。

现在您可以手动分配变量来更改这些值,但我认为使用Zend GData结构化返回对象更好。

在任何情况下:

  1. 从谷歌获取你想要发布的文件。
  2. 找到正确的条目(在这种情况下,与ID https://docs.google.com/feeds/id/spreadsheet条目:$ theId /修改/ 1)
  3. 改变文档:发布value属性为“true”
  4. 发送PUT请求与修改的条目

应该工作

+0

感谢您的回复。你能否详细说明并提供这个工作的例子?我现在正在收到无效的请求URI错误。 – 2011-06-30 22:19:39