2

I'm getting an error, "The requested URL returned error: 401 Unauthorized", when attempting to use PHP cURL with Google Apps Script.

I've been following the following guides, but nothing seems to work:
How do I post to a Google script with cURL in PHP and return text?
Posting data to google apps script using php/curl

I've posted this additional question yesterday which helped me figure out that I needed to get my SSL certificates all setup:
How to use Google Scripts with cURL PHP? [duplicate]

My test.php page:

<div id="body">
    <form name="testing" method="post">
        Email: <input type="text" name="email"><br>
        <input type="submit" value="Submit" onClick="return OnTest1();">
    </form>
</div>

<script>
    function OnTest1()
    {
        document.testing.action = "testsubmit.php";
        document.testing.submit();
        return true;
    }
</script>

My testsubmit.php page:

<?php   
    $url = 'https://script.google.com/a/meditech.com/macros/s/[SCRIPT ID]/exec';
    $data['email'] = $_POST['email'];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_FAILONERROR,true);

    $result = curl_exec($ch);

    if(curl_error($ch))
    {
        echo curl_error($ch);
    }
    else
    {
        echo $result;
    }

    curl_close($ch);
?>

And my Google Apps Script:

function doPost(e)
{
  return ContentService.createTextOutput(e.parameter.email);
}

I want to be able to at some point pass a bunch of information into my php page to get processed into a database I have setup, from there once it's been dumped into the database I need to then send all that info over to the Google Apps Script to then send out an email.

Thank you to whoever can help.

Russ B
  • 35
  • 6
  • Can we ask you about the condition of the deployed Web Apps? Those are ``Execute the app as:`` and ``Who has access to the app:``. – Tanaike Apr 26 '18 at 22:17
  • The app settings are `Execute the app as:` Myself, and `Who has access to the app:` CompanyDomain.com – Russ B Apr 26 '18 at 22:41
  • By recent update, it is required to be shared the project to access to Web Apps. Can you share the project deployed Web Apps and try again? If this was not solution for your situation, I'm sorry. – Tanaike Apr 26 '18 at 22:48
  • Hi @Tanaike the app is shared within my companies domain only. – Russ B Apr 26 '18 at 23:41
  • Thank you for your reply. Can I think that the project with deployed Web Apps has already been shared with users? – Tanaike Apr 27 '18 at 00:33
  • Hi @Tanaike, I'm not sure I understand your question, my apologies. I would think setting the web app policies to `Execute the app as: Myself` and `Who has access to the app: CompanyDomain.com` would be enough right? – Russ B Apr 27 '18 at 03:38
  • Also I worry that I may have not been able to understand about your situation. I'm sorry for my poor English skill. Did you deploy Web Apps by https://developers.google.com/apps-script/guides/web ? – Tanaike Apr 27 '18 at 03:54
  • I did indeed. It's been deployed with a doPost and returns a ContentService value: `function doPost(e) { return ContentService.createTextOutput(e.parameter.email); }` – Russ B Apr 27 '18 at 03:59
  • In my environment, I cannot reproduce your situation yet. So I would like to confirm about the situation. At first, have you ever confirmed whether it can access to the deployed Web Apps by other methods? If you have never done, can you try to access to Web Apps using curl which is not php? curl sample is a simple way. So the wrong points might be found. I'm sorry for my poor skill. – Tanaike Apr 27 '18 at 04:57

1 Answers1

1

Yes, using Execute the app as: Myself and Who has access to the app: CompanyDomain.com is aboslutely fine but at the same time you have to make sure that google identifies you that you're from CompanyDomain.com only. How are you doing that now ?

I think for that you need to pass authentication details with the requset, maybe something like OAuth/2 token. First get token for the user who is making request and then pass that token in Authentication header so that google identifies the user.

Or, if you dont want that then you can switch to Who has access to the app: Anyone, even anonymous then you don't need to prove your identity.

Choose what suits best for your need.

You can check my answer here also How to create a doPost(e) to receive data from an external service?

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34