How can I create an authenticated doPost API with Google Apps Script?

Viewed 896

What I want to realize

Use doPost API with authentication from curl


Now, I'm using an API that automatically creates a google form from POST data and returns the form URL and other data. This is made using GoogleAppsScript doPost.

This publishing method is

Who has access to the app: Anyone, even anonymous

Even if you are not logged in to your google account, you can use the API if you know the url.(Rather, I'm using curl to make an API so I can use it anonymously) Until now, there was no problem because it was operated with a personal google account. But now I need to run this API with my corporate G-suite Google account. In this case, the only option for Who has access to the app: is only myself or an account belonging to the company. Therefore, API cannot be used from anonymous such as cURL command.

How can I prove that I am the owner of this script or a user in my company?

Same with doGet, not just doPost. When a browser logged in with my account makes an HTTP request to the "Current web app URL", the response is returned as expected. If I use an anonymous user like curl, it's natural to redirect to the google account login page.

Is there a way to prove that the user of curl is me or a company person by issuing a token and having it in an HTTP header? Or is there any way to achieve the same thing as my API with some alternatives?

Even if it is not curl, I can use a script written in python or js and so on.

If you are familiar with GAS, have experience, or have some information that may be helpful, please let me know.

Thank you.

And I'm not good at English. Sorry for the bad grammar.


What I tried

I heard that an API with authentication can be created with ExecutionAPI (Apps Script API?). I also challenged. However, the API is not yet available from curl.


Required OAuth scope for this script

https://www.googleapis.com/auth/forms


Source code


function doPost(e) {

    var postData = JSON.parse(e.postData.getDataAsString());
    if (postData.title) {
        const title = postData.title;
        const description = postData.description;
        } else {
        var returnData = ContentService.createTextOutput();
        returnData.setMimeType(ContentService.MimeType.JSON);
        returnData.setContent(
            JSON.stringify({
                message: 'please input title!',
                error: true,
            })
        );
        return returnData;
    }

    // create form
    var form = FormApp.create(title);
    form.setDescription(description);

    var items = postData.items;
    var itemIdList = []

    while (items.length) {
        var item = items.shift();
        var textItem = form.addTextItem();
        textItem
            .setTitle(item.question);
        itemIdList.push({
            question: item.question,
            item_id: textItem.getId(),
        });
    }

    var returnData = ContentService.createTextOutput();
    returnData.setMimeType(ContentService.MimeType.JSON);
    returnData.setContent(
        JSON.stringify({
            published_url: form.getPublishedUrl(),
            edit_url: form.getEditUrl(),
            error: false,
            form_id: form.getId(),
            item_id_list: itemIdList,
        })
    );
    return returnData;
}

POST data example

{
    "title": "Question title",
    "description": "This is a description.",
    "items": [
        {
            "question": "How old are you?"
        },
        {
            "question": "What's your name"
        },
        {
            "question": "Please tell me the phone number"
        }
    ]
}
0 Answers
Related