0

我在将 Google Docs(电子表格)导出到 xls 文件并保存到我们自己的服务器时遇到问题。

成功将文件上传到 Google Docs 后,但在下载时以某种方式收到 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

$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 回答
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 回答