2

Googleドキュメント(特にスプレッドシート)をプログラムで公開する方法について、私は完全に迷っています。

Google Documents List 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>

ドキュメント リスト フィードを取得し、ワークシートを CRUD していますが、パブリッシュを機能させることができず、どのように機能するのか理解できません。フィードへの接続を確立し、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

4 に答える 4

2

ドキュメントがすでに作成されており、ドキュメント ID が XXXX であると仮定します。

必要なことは、特定のヘッダーと XML (ドキュメントを記述するエントリ) を本文として指定した "PUT" 要求を特定の URL に送信することです。

ドキュメントのコンテンツ (メタデータのみ) を変更していないため、ターゲット 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);

幸運を!これが役立つかどうか教えてください!

于 2011-07-21T21:18:03.640 に答える
1

わかりました...どこから始めますか?

まず第一に、あなたのURLは間違っています。(使用しているリソースIDはURLではなくJSON / XML用です)

あなたが持っている

$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要求を実行できなくなります。

これらの値を手動で変更して変数を割り当てることができるようになりましたが、ZendGData構造化された戻りオブジェクトを使用する方がよいと思います。

とにかく:

  1. 公開したいドキュメントをグーグルから取得します。
  2. 正しいエントリを見つけます(この場合、IDがhttps://docs.google.com/feeds/id/spreadsheet:$ theId / Revisions / 1のエントリ)
  3. docs:publishvalueプロパティを「true」に変更します
  4. 変更されたエントリを使用してputリクエストを送信します

それはうまくいくはずです

于 2011-06-21T02:10:49.217 に答える
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
);

これが役に立てば幸いです。

ギャリー

于 2011-04-20T16:37:32.247 に答える
0

Google は、入力されたデータが間違っていると言って、コード 400 で応答します。

このコードを配置してみてください

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

<entry  xmlns='http://www.w3.org/2005/Atom'...
于 2011-05-06T12:49:45.727 に答える