Google文档列表API - 如何发布文档

时间:2011-04-18 20:41:04

标签: gdata-api gdata google-docs-api put zend-gdata

我完全不知道如何以编程方式发布Google文档(特别是电子表格)。

我已阅读Google文档列表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>user@gmail.com</email>
  </author>
  <docs:publish value="true"/>
  <docs:publishAuto value="false"/>
</entry>

我一直在检索文档列表供稿和CRUDing工作表,但我不能让发布工作也不能理解它应该如何工作。我建立与Feed的连接并准备数据为PUT的基本设置如下:

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

4 个答案:

答案 0 :(得分:2)

假设文档已经创建并且文档ID为XXXX

您需要做的是将具有特定标头的“PUT”请求和作为正文的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);
祝你好运! 如果这有帮助,请告诉我!

答案 1 :(得分: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";

(如果使用“as delimiters”,你可以省略。连接)

现在还有其他问题,因为您手动创建了一个xml条目。

  1. 您的授权标题丢失。
  2. 在您的XML中,您使用的是修订版1,但在您的网址中,您有修订版/ 0
  3. 值是手动编写的,我很确定你没有尝试发布一个2岁的文件。同样和
  4. 必须匹配检索到的etag,否则您将无法执行任何PUT请求。
  5. 现在您可以手动更改这些值来分配变量,但我认为最好使用Zend GData结构化返回对象。

    无论如何:

    1. 从谷歌检索您要发布的文档。
    2. 找到正确的条目(在这种情况下,条目为https://docs.google.com/feeds/id/spreadsheet:$ theId / revisions / 1)
    3. 更改文档:将值属性发布为“true”
    4. 发送带有修改条目的放置请求
    5. 应该有效

答案 2 :(得分:0)

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

我不知道这是你的意思,但这是我的代码:

$client = Zend_Gdata_ClientLogin::getHttpClient(
    'my@googleDocsEmail.address', 
    '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
);

我希望这有帮助,

加里

答案 3 :(得分:0)

Google表示推送的数据错误,并使用代码400回复您。

尝试放置此代码

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

之前

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