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条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?