2011-04-18 111 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