2015-05-27 17 views
8

ive state utilizzando Zend Gdata per un po 'di tempo, e oggi im ottenere un errore dizend GData e Google foglio di calcolo non si collega

Notice: Undefined offset: ClientLogin.php on line 150 

tramite PHP, questo ha lavorato per un po' di tempo, e oggi senza cambiare qualsiasi cosa abbia smesso di funzionare, indovinando un servizio deprecato per conto di google con zend gdata, forse il metodo Zend_Gdata_ClientLogin::getHttpClient() o qualcosa del genere, qualcuno può confermare o aiutarmi con questo problema. il codice im utilizzata per connettersi è come segue:

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

Sto usando il gdata zend con i fogli google

anche i punti di errore specificamente per questa linea

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

come ho detto, stavo usando questo per un po 'ora, e nulla è cambiato sulla mia fine

risposta

0

Alla fine sto finendo con qualcosa di simile a questo (codice molto grezzo finora, ma abbastanza per coloro che stanno cercando la soluzione.Avete bisogno di php google foglio di calcolo client di https://github.com/asimlqt/php-google-spreadsheet-client). Questo è un piccolo esempio di inserimento di uno fila al mio foglio di calcolo (mi dispiace per il mio codice, ma sho ala esempio solo lavorando) Grazie per Bram Brambring mostrare il modo migliore per l'autorizzazione - answer by 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 = '[email protected]om'; 
     $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 :) 

    } 
} 

E nel modello principale sto usando:

$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, grazie a Bram Brambring per il suo gettone soluzione, sembra più facile del mio. Lavorando come un incantesimo ora, spero che il suo modo di aggiornare il token normalmente. GRAZIE AMICO!

+0

Maxim Fedan, se puoi, visita la mia altra domanda [link] http : //stackoverflow.com/questions/30532607/copyfile-function-google-api-php se mi può aiutare con quello per favore – nonaxanon

+0

non ha davvero funzionato con google drive api e mai copiato/creato/rimosso :(mi dispiace –

-2

Ho anche avuto script php utilizzando Zend per fogli di calcolo di Google. Stanno andando alla grande per anni, ma hanno smesso di lavorare oggi a mezzogiorno senza motivo. Cosa è cambiato da parte di Google e come risolverlo facilmente?

0

stato bloccato con questo problema aswell, sembra che Google ClientLogin è stato finalmente rimosso

Important: Do not use ClientLogin for new applications. Instead, use the more secure OAuth authentication protocol. ClientLogin is a deprecated authentication protocol and is being turned down on April 20, 2015. At that time, ClientLogin requests will no longer be answered. If you have existing applications that use ClientLogin, we encourage you to migrate to OAuth. The ClientLogin support in this library will be removed in the next major release.

andando a cercare rifare i miei fogli di calcolo con OAuth ora, questa potrebbe essere la soluzione: rimosso perchè non più reputazione, posterò collegamento in commets

modifica; avendo qualche problema con magnetikonline, quindi proverò questa soluzione: rimosso cuz non più reputazione, posterò il link nei commenti

edit2;/'\ questa soluzione è molto meglio, devo andare ora, ma ho avuto un certo successo con questa libreria, funziona molto meglio per quanto vedo e fornisce più funzionalità, provalo.

Per il tuo problema con il token, prova https://github.com/asimlqt/php-google-oauth. Questo ha anche funzionato per me ed è molto più semplice, ho presentato array con le mie informazioni token (se proverai quel token con la seconda libreria, il tuo token diventerà array, mentre hai bisogno solo di $ accessToken ['access_token'] parte per fare $ ServiceRequest = new DefaultServiceRequest ($ access token);

un altro grande tutorial che ho trovato: http://konstantinshkut.com/blog/2014/11/01/how_to_get_data_from_google_spreadsheet_in_yii_php_application (thats per seconda soluzione, asimlqt/php-google-foglio-client), il punto 6 aiuta molto a capire come il mio file deve

+0

Uso: exchangecodefortokens.php -c CODICE DI AUTORIZZAZIONE Im bloccato lì, pensi che tu possa condividere alcuni suggerimenti? – nonaxanon

+0

Il codice deve essere alla ricerca come 4/z7xfjN3RI3dNTy38cfMzu4zF9pJNrJxCk2m-55LA.cqdvdLrNQ335uyeEpmwI #, che mi stava dando errore fino a quando ho provato ad usare solo una parte prima del punto, quindi nel mio caso 4/z7xfjN3RI3dNTy38cfMzu4zF9pJNrJxCk2m-55LA (php exchangecodefortorkens.php -c 4/z7xfjN3RI3dNTy38cfMzu4zF9pJNrJxCk2m -55LA) –

+0

ok ma come faccio a fare lo scambio di php ... questo è quello che non ho idea – nonaxanon

4

I utilizza ClientLogin per un'applicazione server-server.

necessario per passare a oAuth2 in fretta oggi. Ho fuso alcuni esempi che ho trovato per ottenere il token di autorizzazione utilizzando un 'account del servizio'

function get_token() { 
$client_email = '0-1.apps.googleusercontent.com'; 
$client_email = '[email protected]'; 
$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; 
} 

avevo bisogno di usare la non-mail sviluppatore dell'app e-mail, lo stesso e-mail deve essere aggiunto come un utente per il foglio di calcolo

il token generato può quindi essere utilizzato con PHP-google-foglio-client

$accessToken = get_token(); 

$serviceRequest = new DefaultServiceRequest($accessToken); 
ServiceRequestFactory::setInstance($serviceRequest); 

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); 
$spreadsheetFeed = $spreadsheetService->getSpreadsheets(); 

$spreadsheet = $spreadsheetFeed->getByTitle('Hello World'); 
+0

potresti spiegare come hai generato il file "abc.p12"? Quando ho creato l'account di servizio, ha scaricato un file .json. Inoltre, quali file di dipendenza sono necessari per la tua funzione get_token()? grazie! – Concept211

+1

il file .p12 viene generato in la tua console api, vai su google type api console, di solito il primo link, accedi con l'account corrispondente al servizio e crea id client, account di servizio, scaricherà automaticamente un file con estensione come .p12 non cambia il nome del file ,,, il nome del file è super lungo – nonaxanon

-3

che deprecato ClientLogin, funziona di nuovo! È stata una tale sorpresa alla vigilia della conferenza Google I/O? Ho dovuto scrivere una soluzione fatta da sé. Le librerie di terze parti erano praticamente inutilizzabili, Ho scritto circa 10 metodi, penso che sia meglio delle alternative, che sono troppo complesse e ingombranti. È possibile acquistare da me)) Non ancora pronto per mettere su github)

Problemi correlati