[Python] HTTP POST from Apps Script to Google Cloud Run Function

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm quite a beginner in Google Cloud Platform, and I would like to solve the following:

    I want to send an HTTP POST request (with JSON data) to a Cloud Run Function using Apps Script, but I don't see any such entry in the Cloud Run Function logs. The Apps Script API is enabled in my GCP project, and I have also provided the GCP project number in the Project settings.

    The roles associated with my account: Owner, Cloud Run Source Developer, Service Account User, Service Usage Consumer

    The roles associated with the Service Account I specifically created for Cloud Run Functions: Cloud Functions Developer, Cloud Run Admin, Service Account Token Creator, Service Account User

    Some info from my code:

    appsscript.json:

    ...
    "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/cloud-platform",
    "openid"
    ],
    ...


    The request parameters, and the request:

    var options = {
    "method": "post",
    "contentType": "application/json",
    "payload": jsonData,
    "headers": {
    "Authorization": "Bearer "+ScriptApp.getIdentityToken() // gcloud auth print-identity-token
    }
    };

    ...

    // url: Cloud Run Functions URL
    var response = UrlFetchApp.fetch(url, options);


    My simple Cloud Run Functions Python code that would handle the request:

    import functions_framework

    @functions_framework.http
    def http_function(request):
    content_type = request.headers["content-type"]
    if content_type == "application/json":
    try:
    request_json = request.get_json(silent=True)
    data = request_json

    except Exception as e:
    return "No JSON data provided", 400

    return f"{data}"


    request payload: {"name":"developer"}

    content-type in the request param: "application/json"

    response header:

    { Date: 'Fri, 04 Apr 2025 09:20:23 GMT', 'Content-Length': '181', 'Content-Type': 'text/html; charset=utf-8', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'X-Cloud-Trace-Context': 'e85c4f564777ce1ae6d02cd31da8d566;o=1', Server: 'Google Frontend' }

    I created a function to test the request (with ScriptApp.getIdentityToken()) and print the response:

    HTTP Response Code: 500


    HTTP Response Text: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
    ​

    In fact, I also tried using Appscript.GetOAuthToken() as recommended by @TheMaster:

    HTTP Response Code: 401, and the HTTP response text is:


    Error: Unauthorized … Your client does not have permission to the requested URL
    ​

    In gcloud shell I tried the following 'CLI test command' that I found in the 'Test service' section:

    curl -X POST https://[service URL] -H "Authorization: bearer $(gcloud auth print-identity-token)" -H "Content-Type: application/json" -d '{"name": "Developer"}'

    This is the output, so the function itself is working correctly: {"name":"Developer"}

    Can someone help me to finally get my function to accept the request?

    If I missed sharing any important info, please let me know.


    SOLVED

    @TheMaster's suggestion solved my issue: I added the Cloud Run Admin and Cloud Run Invoker roles to my user account, and I was able to send an HTTP POST request, which returned a 200 status code.

    Continue reading...

Compartilhe esta Página