Zend gdata和谷歌电子表格不连接


zend gdata and google spreadsheet not connecting

我使用Zend Gdata已经有一段时间了,今天我得到了一个错误

Notice: Undefined offset: ClientLogin.php on line 150

通过php,这已经工作了一段时间了,今天没有改变任何东西,它停止了工作,我猜一些过时的服务代表谷歌与zend gdata可能是Zend_Gdata_ClientLogin::getHttpClient( )方法或其他东西,可以有人确认或帮助我解决这个问题。我用来连接的代码如下:

    require_once('Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Docs');
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
require_once 'Zend/Gdata.php';
require_once 'Zend/Gdata/AuthSub.php';
require_once 'Zend/Gdata/Spreadsheets.php';
require_once 'Zend/Gdata/Spreadsheets/DocumentQuery.php';
require_once 'Zend/Gdata/Spreadsheets/ListQuery.php';
require_once 'Zend/Loader.php';

$sourceUser = "myemail";
$sourcePass = "mysuperawesomepassword";
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser, $sourcePass, $service);
$connection = new Zend_Gdata_Spreadsheets($sourceClient);

我正在使用zend gdata与谷歌电子表格

错误也指向这一行

$sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser, $sourcePass, $service);

就像我说的,我使用这个已经有一段时间了,在我的端没有任何改变

我使用ClientLogin作为服务器到服务器的应用程序。

今天需要匆忙切换到oAuth2。我合并了一些我发现使用"服务帐户"获得授权令牌的示例

function get_token() {
$client_email = '0-1.apps.googleusercontent.com';
$client_email = '0-1@developer.gserviceaccount.com';
$private_key = file_get_contents('abc.p12');
$scopes = array('https://spreadsheets.google.com/feeds');
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key,
    'notasecret',                                 // Default P12 password
    'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
 $client->getAuth()->refreshTokenWithAssertion();
}
 $tokenData = json_decode($client->getAccessToken());
        return $tokenData->access_token;
}

我需要使用开发人员的电子邮件,而不是应用程序的电子邮件,相同的电子邮件必须作为用户添加到电子表格

生成的令牌可以使用php-google-spreadsheet-client
$accessToken = get_token();
$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google'Spreadsheet'SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle('Hello World');

最后,我以这样的东西结束(到目前为止非常原始的代码,但对于那些正在寻找解决方案的人来说已经足够了。您需要php谷歌电子表格客户端https://github.com/asimlqt/php-google-spreadsheet-client)。这是插入一行到我的电子表格的小例子(对不起我的代码,但显示只是工作的例子)感谢bram brambring展示了更好的授权方式- bram brambring的回答

<?php
/*
 * Google Spreadsheet class to work with google spreadsheets obviously ;D [using OAuth 2.0, as Zend Gdata is not anymore working]
 */
require_once('/Google/Spreadsheet/ServiceRequestInterface.php');
require_once('/Google/Spreadsheet/DefaultServiceRequest.php');
require_once('/Google/Spreadsheet/ServiceRequestFactory.php');
require_once('/Google/Spreadsheet/Spreadsheet.php');
require_once('/Google/Spreadsheet/SpreadsheetFeed.php');
require_once('/Google/Spreadsheet/SpreadsheetService.php');
require_once('/Google/Spreadsheet/Exception.php');
require_once('/Google/Spreadsheet/UnauthorizedException.php');
require_once('/Google/Spreadsheet/Spreadsheet.php');
require_once('/Google/Spreadsheet/Util.php');
require_once('/Google/Spreadsheet/Worksheet.php');
require_once('/Google/Spreadsheet/WorksheetFeed.php');
require_once('/Google/Spreadsheet/ListFeed.php');
require_once('/Google/Spreadsheet/ListEntry.php');
require_once('/Google/Spreadsheet/CellFeed.php');
require_once('/Google/Spreadsheet/CellEntry.php');
require_once('/Google/Config.php');
require_once('/Google/Client.php');
require_once('/Google/Auth/Abstract.php');
require_once('/Google/Auth/OAuth2.php');
require_once('/Google/Http/Request.php');
require_once('/Google/Utils.php');
require_once('/Google/IO/Abstract.php');
require_once('/Google/IO/Curl.php');
require_once('/Google/Http/CacheParser.php');
require_once('/Google/Logger/Abstract.php');
require_once('/Google/Logger/Null.php');
require_once('/Google/Exception.php');
require_once('/Google/Auth/Exception.php');
require_once('/Google/Auth/AssertionCredentials.php');
require_once('/Google/Cache/Abstract.php');
require_once('/Google/Cache/File.php');
require_once('/Google/Signer/Abstract.php');
require_once('/Google/Signer/P12.php');
use Google'Spreadsheet'DefaultServiceRequest;
use Google'Spreadsheet'ServiceRequestFactory;
class Google_Spreadsheet
{
    private $default = array(
        'worksheetCols' => 12,
        'worksheetRows' => 25
    );
    private $spreadsheetKey;
    private $spreadsheetName;
    private $worksheetName;
    private $spreadsheetFeed;
    public $initialized = true;
    public function __construct($spreadsheetKey, $worksheetName, $spreadsheetName = '')
    {
        $this->spreadsheetKey = $spreadsheetKey;
        $this->worksheetName = $worksheetName;
        $this->spreadsheetName = $spreadsheetName;
        $this->initialized = $this->initialize();
        return true;
    }
    private function getToken() {
        $client_email = '318977712937456456454656563tcfjblgoi@developer.gserviceaccount.com';
        $private_key = file_get_contents('API Project-f10e456456b60.p12');
        $scopes = array('https://spreadsheets.google.com/feeds');
        $credentials = new Google_Auth_AssertionCredentials(
            $client_email,
            $scopes,
            $private_key,
            'notasecret',                                 // Default P12 password
            'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type
        );
        $client = new Google_Client();
        $client->setAssertionCredentials($credentials);
        if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion();
        }
        $tokenData = json_decode($client->getAccessToken());
        return $tokenData->access_token;
    }
    public function initialize(/*$reInitialized = false*/)
    {
        // load OAuth2 token data - exit if false
        $tokenData = $this->getToken();
        $serviceRequest = new DefaultServiceRequest($tokenData);
        ServiceRequestFactory::setInstance($serviceRequest);
        $spreadsheetService = new Google'Spreadsheet'SpreadsheetService();
        try {
            $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
        } catch ('Google'Spreadsheet'UnauthorizedException $e) {    
            Google_Spreadsheet::warnAdmin($e->getMessage());
            return false;
        }
        $this->spreadsheetFeed = $spreadsheetFeed;
        return true;
    }
    public function insertRow($rowData, $default_fields = array()) {
        $spreadsheetFeed = $this->spreadsheetFeed;
        $spreadsheet = $this->spreadsheetKey ? $spreadsheetFeed->getByKey($this->spreadsheetKey) : $spreadsheetFeed->getByTitle($this->spreadsheetName);
        if(!$spreadsheet && !empty($this->spreadsheetName)) {
            $spreadsheet = $spreadsheetFeed->getByTitle($this->spreadsheetName);
        }
        if(!$spreadsheet) {
            Google_Spreadsheet::warnAdmin('No spreadsheet', serialize($rowData));
            return false;
        }
        $worksheetFeed = $spreadsheet->getWorksheets();
        $worksheet = $worksheetFeed->getByTitle($this->worksheetName);
        if(!$worksheet) {
            //create worksheet if not exist
            $worksheet = $spreadsheet->addWorksheet($this->worksheetName, $this->default['worksheetRows'], $this->default['worksheetCols']);
            $cellFeed = $worksheet->getCellFeed();
            for( $i= 1 ; $i <= $this->default['worksheetCols']; $i++ ) {
                if(isset($default_fields[$i])) {
                    $cellFeed->editCell(1, $i, $default_fields[$i]);
                }
                else {
                    $cellFeed->editCell(1, $i, "head");
                }
                $cellFeed->editCell(2,$i,"content");
            }
        }
        if(!$worksheet) {
            Google_Spreadsheet::warnAdmin('No worksheet', serialize($rowData));
            return false;
        }
        $listFeed = $worksheet->getListFeed();
        $data = array();
        foreach ($listFeed->getEntries() as $entry) {
            $values = $entry->getValues();
            $data[] = $values;
            break; //only first row needed, as we need keys
        }
        $keys = array();
        if(!count($data)) {
            Google_Spreadsheet::warnAdmin('No data', serialize($rowData));
            return false;
        }
        foreach ($data[0] as $key => $value) {
            $keys[] = $key;
        }
        $newRow = array();
        $count = 0;
        foreach($keys as $key) {
            if(isset($rowData[$count])) {
                $newRow["$key"] = $rowData[$count];
            }
            else {
                $newRow["$key"] = '';
            }
            $count++;
        }
        $listFeed->insert($newRow);
        return true;
    }

    static function warnAdmin($reason = '', $content = '') {
        //temporal function to warn myself about all the stuff happening wrong :)
    }
}

在主模型中,我使用:

$spreadsheet = new Google_Spreadsheet("spreadsheet name or ID", $worksheetname, "My spreadsheet name");
        if(!$spreadsheet->initialized) {
            Google_Spreadsheet::warnAdmin('cannot initialize spreadsheet', serialize($rowValues));
        }

        if(!$spreadsheet->initialized || !$spreadsheet->insertRow($rowValues, $this->default_fields)) {
            Google_Spreadsheet::warnAdmin('failed to insert row ');
        }
@Edit,感谢bram brambring的令牌解决方案,似乎比我的更容易。现在很有魅力,我希望他的方式能正常刷新token。谢谢你!

一直被这个问题困扰,似乎谷歌的ClientLogin终于被删除了

重要:不要在新的应用程序中使用ClientLogin。相反,使用更安全的OAuth认证协议。ClientLogin是一个已弃用的身份验证协议,并在4月被拒绝20日,2015年。那时,ClientLogin请求将不再是回答。如果您有使用ClientLogin的现有应用程序,我们建议您迁移到OAuth。其中的ClientLogin支持库将在下一个主要版本中删除。

现在要尝试用OAuth重新制作我的电子表格,这可能是解决方案:删除,因为没有更多的声誉,将链接发布到评论

编辑;磁性在线遇到了一些麻烦,所以要尝试这个解决方案:删除,因为没有更多的声誉,将链接发布到评论

edit2;这个解决方案要好得多,我现在必须走了,但是我已经在这个库上取得了一些成功,就我所看到的和提供的功能而言,它的工作效果要好得多,试试吧。

对于token的问题,请尝试https://github.com/asimlqt/php-google-oauth。这种方式也适用于我,它更容易,我收到数组与我的令牌信息(如果你要尝试第二个库令牌,你的令牌将是数组,而你只需要$accessToken['access_token']部分,以使$serviceRequest =新的DefaultServiceRequest($accessToken);

我发现的另一个大教程:http://konstantinshkut.com/blog/2014/11/01/how_to_get_data_from_google_spreadsheet_in_yii_php_application(这是第二个解决方案,asimlqt/php-google-spreadsheet-client),第6步有助于理解我的文件必须看起来。

我也有php脚本使用Zend谷歌电子表格。多年来一直运转良好,但今天中午无故停止工作了。谷歌那边发生了什么变化?如何轻松解决?

已弃用的ClientLogin,它又可以工作了!在谷歌I/O大会前夕,这真是一个惊喜。我必须写一个自制的解。第三方库基本上是无法使用的,我写了大约10种方法,我认为它比其他的方法要好,这些方法太复杂和繁琐了。你可以从我这里买还没有准备好放在github上)