Want to add new record/row in Google spreadsheet in PHP -


i want add new records in google spreadsheet php coding. had searched , found solution using gmail id , password system authentication. working after 2 days stop working. code mentioned below:

<?php  include 'spreadsheet.php'; $spreadsheet = new spreadsheet("xxxxx@gmail.com", "xxxxxxx"); $spreadsheet->setspreadsheet("tester")->setworksheet("sheet1")->add(array("first name" => "cell 1", "last name" => "cell 2"));  ?> 

after stops working came know google has changed it's login system , need migrate oauth system authentication.

after doing long r & d found 1 example - "https://github.com/asimlqt/php-google-spreadsheet-client" . not working , after combing various source of searching have develop following code:

<?php  include_once "google-api-php-client/examples/templates/base.php";  /************************************************   make api request authenticated service   account.  ************************************************/ require_once realpath(dirname(__file__) . '/google-api-php-client/src/google/autoload.php');  $accesstoken = getgoogletokenfromkeyfile("xxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxx");  use google\spreadsheet\defaultservicerequest; use google\spreadsheet\servicerequestfactory;  //servicerequestfactory::setinstance(new defaultservicerequest($accesstoken));  // load spreadsheet , worksheet $worksheet = (new google\spreadsheet\spreadsheetservice())     ->getspreadsheets()     ->getbytitle('sheet1')       // spreadsheet name     ->getworksheets()     ->getbytitle('tester');      // worksheet name $listfeed = $worksheet->getlistfeed();  // uncomment find out google calls column names // print_r($listfeed->getentries()[0]->getvalues());  // add new blank row spreadsheet, using column headings $listfeed->insert(['name' => 'simon', 'age' => 25, 'gender' => 'male']);  /**  * retrieves google api access token using p12 key file,  * client id , email address  *  * these 3 things may obtained   * https://console.developers.google.com/  * creating new "service account"  */ function getgoogletokenfromkeyfile($clientid, $clientemail, $pathtop12file) {     $client = new google_client();     $client->setclientid($clientid);      $cred = new google_auth_assertioncredentials(         $clientemail,         array('https://spreadsheets.google.com/feeds'),         file_get_contents($pathtop12file)     );      $client->setassertioncredentials($cred);      if ($client->getauth()->isaccesstokenexpired()) {         $client->getauth()->refreshtokenwithassertion($cred);     }      $service_token = json_decode($client->getaccesstoken());     return $service_token->access_token; }   ?> 

but unfortunately 1 not working , after sudden timeframe showing request time out error in local xampp server.

and till date application on hold. not sure now. if has concrete solution against please share me. main purpose add data google spreadsheet when user submits details in website. or if not possible after google's change in authentication system please confirm me also. want reachout final solution of problem.

thanks in advance.

if able retrieve valid token, pass token in request header "bearer yourtokenstring". google changed "googlelogin auth=yourtokenstring". use bearer tag instead.

if struggling in effort retrieve valid access token, consider using service account p12 key file. grant edit privileges spreadsheet service account email address.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -