0

First of all sorry for bad english. I am using Google Sheets API for my web project and I am following Google Sheets API documentation. OAuth 2.0 flow is going well and I am getting access token but when control reach the $response = $service->spreadsheets_values->get($spreadsheetId,$range); it shows localhost is currently unable to handle this request. HTTP ERROR 500. I am also using ngrok to expose my localhost to google

    require_once APPPATH."third_party/googleClientLibrary/vendor/autoload.php";
    session_start();
    $client = new Google_Client();
    $client->setAuthConfig(APPPATH."third_party/googleClientLibrary/client_secret.json");
    $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY); 
    $client->setAccessType('offline');
    $client->authenticate($_GET['code']);
    $_SESSION['access_token']= $client->getAccessToken();

     if (isset($_SESSION['access_token'])) 
     {
    $client->setAccessToken($_SESSION['access_token']);
    $service = new Google_Service_Sheets($client);
    $spreadsheetId = '1Wr_6u4fO7NNWHeYsdTm0S-z9UU3NtPMdwIhkrSAfEpA';
    $range = 'Test-sheet!A:E';
    $response = $service->spreadsheets_values->get($spreadsheetId,$range);
    $values = $response->getValues();
    }`
SH_
  • 266
  • 2
  • 3
  • 17
hassan
  • 5
  • 5
  • Why don't you do the authentication and creation of tokens in the day the [quickstart](https://developers.google.com/sheets/api/quickstart/php) recommends? – ziganotschka Apr 30 '20 at 11:46
  • THanks for your reply . Actually i want tp perform this task for web app . and i am following this link https://developers.google.com/identity/protocols/oauth2/web-server can you help me? i am beginner i will be thankfull for this act of kindness – hassan Apr 30 '20 at 15:51
  • It's better if you follow [this workflow](https://developers.google.com/identity/protocols/oauth2/web-server#php_2). You need to implement `oauth2callback.php`. Why do you use `setSCoeps` instead of `$client->addScope` as in the documentation. Did you download the correct credentials for a WebApp from the GCP? – ziganotschka Apr 30 '20 at 16:03
  • acording to your suggestion i changed set scope to add scope but result is still same actually code i mentioned above is in redirect uri when user grant the permissions then control comes in this block of code yeah i downloaded correct credentials even i have saw access token with refresh token using var dump every thing is going good user grant permissons then after this i am getting authorization code and using this code i am also getting access tokens but when control reaches at $response = $service->spreadsheets_values->get($spreadsheetId,$range) it throws error http 500 – hassan Apr 30 '20 at 17:10
  • can you help me?i am still here – hassan May 01 '20 at 03:48
  • 500 error is difficult to troubleshoot. Sometimes people get 500 errors when forgetting to enable the API in the GCP: https://stackoverflow.com/questions/57746928/error-500-at-google-spreadsheets-values-get-method. Have also a look [here](https://stackoverflow.com/questions/40216761/the-localhost-page-isn-t-working-localhost-is-currently-unable-to-handle-this-re) – ziganotschka May 01 '20 at 05:48
  • 1
    hi bro hopefully you will be fine i have fixed this issue i was using latest release of php client library some functions of this are not compatible with php version 5.5 so i fixed this issue by using older release of php client library .We can also solve this issue by upgrading over version to php 7 thanks for you assitance stay bless – hassan May 03 '20 at 07:52

1 Answers1

0

I was using php client library realease 2.4.1 and some functions of this library requires php version 7 . My php version is 5 so i used older release of client library

hassan
  • 5
  • 5