connect like this:

require_once('Zend/Gdata/Spreadsheets.php'); require_once('Zend/Loader.php'); $loader = new Zend_Loader(); $loader->loadClass('Zend_Gdata'); $loader->loadClass('Zend_Gdata_Query'); $loader->loadClass('Zend_Gdata_AuthSub'); $loader->loadClass('Zend_Gdata_ClientLogin'); $loader->loadClass('Zend_Gdata_Spreadsheets'); $loader->loadClass('Zend_Gdata_Spreadsheets_DocumentQuery'); $loader->loadClass('Zend_Gdata_Spreadsheets_CellQuery'); $g_service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME; $client = Zend_Gdata_ClientLogin::getHttpClient($login, $password, $g_service); 

The fact is that literally two weeks ago everything worked, and now it gives an error: Undefined offset: 1 in Zend\Gdata\ClientLogin.php(151)

offset 1 is needed in splitting a string by the "=" symbol:

  foreach (explode("\n", $response->getBody()) as $l) { $l = chop($l); if ($l) { list($key, $val) = explode('=', $l, 2);// вот здесь $goog_resp[$key] = $val; } } 

but $response->getBody() returns https://developers.google.com/accounts/docs/AuthForInstalledApps\n , where there is no "="!

PS Google’s account allows connections from any location and from any applications.

  • Understood) Google completely switched to oAuth 2.0, so the library Zend GData is outdated. It is necessary to download the Google library. - Boeny
  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Nicolas Chabanovsky

1 answer 1

Google switched to oAuth 2.0 , due to which the order of connection to the account has changed.

  1. Create a new project at https://console.developers.google.com

  2. Go to it and select APIs & auth -> APIs on the left (you don’t need to add anything to work with Google Docs , but if you want to be able to create or delete files, you need to connect the Drive API )

  3. Select APIs & auth -> Credentials and generate keys by clicking on Create New Client ID (we will also need Email address and file with extension P12 - generated there as well)

  4. IMPORTANT! To write to a table / document, you need to insert the generated email into the Invite people field in the document interface (Access Settings)

  5. Download the Google Library https://developers.google.com/discovery/libraries

  6. We import the following necessary classes into my class (I have it SpreadSheet ):

     require_once('Google/Loader.php'); $loader = new Google_Loader(); $loader->loadClass('Google_Config'); $loader->loadClass('Google_Client'); $loader->loadClass('Google_Auth_Abstract'); $loader->loadClass('Google_Auth_OAuth2'); $loader->loadClass('Google_Auth_AssertionCredentials'); $loader->loadClass('Google_Cache_Abstract'); $loader->loadClass('Google_Cache_File'); $loader->loadClass('Google_Logger_Abstract'); $loader->loadClass('Google_Logger_Null'); $loader->loadClass('Google_Utils'); $loader->loadClass('Google_Signer_Abstract'); $loader->loadClass('Google_Signer_P12'); $loader->loadClass('Google_Http_Request'); $loader->loadClass('Google_IO_Abstract'); $loader->loadClass('Google_IO_Curl'); $loader->loadClass('Google_Http_CacheParser'); $loader->loadClass('Google_Exception'); $loader->loadClass('Google_Auth_Exception'); 
  7. we connect:

     private function get_token() { // читаем скачанный файл *.P12 $private_key = file_get_contents('gpk/crm0.p12'); // используем сгенеренный email и scope - ссылку на API // (для таблиц это https://spreadsheets.google.com/feeds) $credentials = new Google_Auth_AssertionCredentials( $this->service_email, $this->get_url(), $private_key, 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', false, false ); $this->client = new Google_Client(); $this->client->setAssertionCredentials($credentials); if ($this->client->getAuth()->isAccessTokenExpired()) $this->client->getAuth()->refreshTokenWithAssertion(); $tokenData = json_decode($this->client->getAccessToken()); // access_token мы будем добавлять к каждому запросу вот так: // '...[url]...?access_token='.$this->token $this->token = $tokenData->access_token; } 

Sending request:

  private function send_request($url, $method='GET', $post_body=null, $save_request=false) { // добавляем версию API в заголовок $header = ['GData-Version' => '3.0']; // добавляем к запросу access_token, полученный ранее $url .= strpos($url, '?') === false ? '?' : '&'; $url .= 'access_token='.$this->token; // для получения данных используется GET, // для обновления - PUT (в $post_body запихивать обновляемые данные в виде xml) // для вставки - POST (в $post_body запихивать вставляемые данные в виде xml) // для удаления - DELETE if ($method == 'POST' || $method == 'PUT') $header['Content-Type'] = 'application/atom+xml'; $request = new Google_Http_Request($url, $method, $header, $post_body); $request = $this->client->getAuth()->authenticatedRequest($request); if ($request->getResponseHttpCode() == 200) { // собственно, полезная информация в виде xml $request = $request->getResponseBody(); if ($save_request) $this->last_request = $request; // парсим xml в объект ObjectSimpleXML return simplexml_load_string($request); } return false; } 
  1. Here (English) tells you in detail what kind of requests you need to send Google_API , Google_API_for_spreadsheets , for_worksheets

  2. Here you can see how Google responds to these requests in person: https://developers.google.com/oauthplayground/