douquan3294 2012-05-21 15:34
浏览 46
已采纳

PHP Zend导出谷歌文档与401 Unauthorized错误消息

I am running a problem with exporting a Google Docs (spreasheet) to xls file and save to our own server.

After successfully uploaded a file to Google Docs but somehow getting the 401 error message when I do the download.

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>

Here is our PHP code and hope someone could help me to point out what went wrong.

Any thoughts would be highly received.

<?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);
    }
}


?>

UPDATE - 23/05/2012

We finally got it working by tweaking the code as below: It would be useful for someone !

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; 
  • 写回答

2条回答 默认 最新

  • duanbo6482 2012-05-23 10:58
    关注

    We have a solution as below:

    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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?