1

I'm trying to figure out why this code won't work with a Google Apps Script web app URL, but it will work with a random web API URL?

The 1st code block gets data from the dummy API. The second code block returns nothing from the Google Apps Script web app URL. The only difference in the code is the $url variable value.

The GAS App is set to Web App so anyone, even anonymous can access it (screenshot below). If I go directly to the GAS url GAS Output, it returns the JSON.

The Google Apps Script code is below in third code block.

Any ideas?

Problem solved thanks to Tanaike!! Working code pasted at the bottom of the question.

enter image description here

First Code Block

<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();

$url = "http://dummy.restapiexample.com/api/v1/employees";

// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);

// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);

curl_close($curl_handle);

// Decode JSON into PHP array
$user_data = json_decode($curl_data);

// Print all data if needed
print_r($user_data);

?>

Second Code Block

<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();

$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";

// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);

// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);

curl_close($curl_handle);

// Decode JSON into PHP array
$user_data = json_decode($curl_data);

// Print all data if needed
print_r($user_data);

?>

First Code Block: GAS Code

function doGet(e) {
  var data = {
    status: 'success',
    dataSet: 'Some Info'

  };

  var output = JSON.stringify(data);
  return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}

Working Code, thank you Tanaike!!

<?php
// Initiate curl session in a variable (resource)
$curl_handle = curl_init();

$url = "https://script.google.com/macros/s/AKfycbyWSDzUyFa41fu_6QWP7h8ToklwWysGZsuSPaRnu649DmPNYG8/exec";



// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);

// was an update from my stack overflow question.
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); //https://stackoverflow.com/questions/59780986/trouble-using-curl-with-google-apps-script-web-app-in-a-php-file/59781600#59781600

// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

// Execute curl & store data in a variable
$curl_data = curl_exec($curl_handle);

curl_close($curl_handle);

// Decode JSON into PHP array
$user_data = json_decode($curl_data);

// Print all data if needed
print_r($user_data);

?>
Mr. B
  • 2,677
  • 6
  • 32
  • 42

2 Answers2

3
  • You want to access to Web Apps using php.
  • You want to know the reason that no values are shown when the script is run.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Reason of your issue:

In your Web Apps, Execute the app as: and Who has access to the app: are set as User accessing the web app and Anyone, respectively. In this case, when you access to Web Apps, it is required to use your access token. So in your script, an error occurs. But curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is used. By this, no values are shown. When curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true) is removed, in your script, the redirect page is retrieved as HTML data.

In order to avoid this issue, how about the following modifications?

Pattern 1:

In your script, it seems that the access token is not used. In this case, please set Execute the app as: and Who has access to the app: as Me and Anyone, even anonymous, respectively. In this case, it is not required to use the access token.

For this, please add the following script to your script for accessing to Web Apps.

curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);

By above flow, you can access to Web Apps using php script and retrieve the values from Web Apps.

Pattern 2:

If you want to access to Web Apps which deployed with Execute the app as: and Who has access to the app: as User accessing the web app and Anyone, respectively, it is required to use the access token.

For this, please add the following script to your script for accessing to Web Apps.

curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
$header = ['Authorization: Bearer ###your access token###'];
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);

Note:

  • Under above condition, if you, who is the owner of Web Apps, makes other users access to your Web Apps with this condition, please share the GAS project that Web Apps was deployed with the users. By this, the users can access to Web Apps with the user's access token.

Note:

  • When you modified the script of Web Apps at Google Apps Script side, please redeploy Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    Tanaike, THANK YOU!!! Your explanation was very helpful and solved my problem. For anyone that comes behind, I needed to add the `curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);` line of code for it to work. Tanaike, can you please explain what that line does for the script? – Mr. B Jan 17 '20 at 16:53
  • 1
    @Mr. B Thank you for replying. I'm glad your issue was resolved. It accesses to Web Apps, the URL is redirected. For this, `CURLOPT_FOLLOWLOCATION` is used. When `CURLOPT_FOLLOWLOCATION` is not used, the redirect page is retrieved as HTML data. – Tanaike Jan 17 '20 at 23:35
0

I tried accessing the Apps Script URL in an incognito window and it redirects to a Google login prompt.

When you use the User accessing the web app setting for the Execute the app as option, the script will, not surprisingly, run as the identity of the user accessing it. Therefore, with your current settings, a user needs to be logged in to a Google account to successfully execute the application.

If you switch the Execute the app as option to Me (<you address>), that makes an additional option, Anyone, even anonymous, available for the Who has access to this app option. That's the only way to enable truly anonymous access to the application.

See the permissions documentation for more details.

chuckx
  • 6,484
  • 1
  • 22
  • 23