0

Google ドキュメント (スプレッドシート) を xls ファイルにエクスポートし、独自のサーバーに保存する際に問題が発生しています。

ファイルを Google ドキュメントに正常にアップロードした後、ダウンロード時に 401 エラー メッセージが表示されます。

Fatal error:  Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 401
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

これが私たちの PHP コードです。何が問題なのかを誰かが指摘してくれることを願っています。

どんな考えでも大歓迎です。

<?php

// set credentials for ClientLogin authentication
$google_user = 'xxxxx'; // Your google account username
$google_pass = 'xxxxx'; // Your google account password

$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME; 
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($google_user, $google_pass,$service);
$docs = new Zend_Gdata_Docs($httpClient);

// Uploading file to Google Docs works perfectly as below
//$newDocumentEntry = $docs->uploadFile('test.txt', 'order-123456','text/plain', Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);


$path = "/var/www/vhosts/googledocs";


$docsQuery = new Zend_Gdata_Docs_Query();
$docsQuery->setTitle("My Spreadsheet Name");
$docsQuery->setTitleExact(false);
$feed = $docs->getDocumentListFeed($docsQuery);

foreach ($feed->entries as $entry) {
    $docID = $entry->getId();
    $docTitle = $entry->getTitle();  


    if($entry->getTitle() == "My spreadsheet File Name"){
        $strURL = $entry->content->getSrc() . '&exportFormat=xls&format=xls';            
        $data = $docs->get($strURL);
        file_put_contents($path."test.xls", $data);
    }
}


?>

更新 - 2012 年 5 月 23 日

以下のようにコードを微調整することで、最終的に機能するようになりました。

foreach($feed as $entry): 
        if($entry->getTitle() == 'My Spreadsheet File Name'){
            $path = "/var/www/vhosts/regustouchstone.com/subdomains/docs/httpdocs/googledocs";
            $strURL = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=".basename($entry->id)."&exportFormat=xls&format=xls";

            try{            
                $data = $service->get($strURL)->getBody();              
                $fp = fopen($path."/test.xls", "w+");
                                fwrite($fp, $data);
                                fclose($fp);                
            }catch(Zend_Exception $e) {
                echo "<br /><br /> ERROR <br />";
                echo $e->getMessage();
            }
        }
    endforeach; 
4

2 に答える 2

0

以下のような解決策があります。

foreach($feed as $entry): 
        if($entry->getTitle() == 'My Spreadsheet File Name'){
            $path = "/var/www/vhosts/regustouchstone.com/subdomains/docs/httpdocs/googledocs";
            $strURL = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=".basename($entry->id)."&exportFormat=xls&format=xls";

            try{            
                $data = $service->get($strURL)->getBody();              
                $fp = fopen($path."/test.xls", "w+");
                                fwrite($fp, $data);
                                fclose($fp);                
            }catch(Zend_Exception $e) {
                echo "<br /><br /> ERROR <br />";
                echo $e->getMessage();
            }
        }
    endforeach; 
于 2012-05-23T10:58:45.280 に答える
0

$strUrl は正しく一貫してエンコードされていますか? たとえば、すべての '&' は '&' としてエンコードされていますか?

また、ダウンロード リンクのサーバーがhttp://spreadsheets.google.com ( http://docs.google.comではなく) である場合は、スプレッドシート サービスに対する認証が必要になる場合があります。

$spreadsheetClient = Zend_Gdata_ClientLogin::getHttpClient($google_user, $google_pass, Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
$spreadSheetService = new Zend_Gdata_Spreadsheets($spreadsheetClient);
...
// download the spreadsheet
$data = $spreadSheetService->get($strURL);
于 2012-05-22T14:06:43.670 に答える