Invoking a Google Cloud Function from a Django View

試著忘記壹切 提交于 2021-01-29 03:34:58

问题


I have created a Google Cloud function that can be invoked through HTTP. The access to the function is limited to the Service account only.

If I had a Django View which should invoke this function and expect a response?

Here is what I have tried

1) Before starting Django I set the environment variable

export GOOGLE_APPLICATION_CREDENTIALS

2) I tried invoking the function using a standalone code, but soon realised this was going nowhere, because I could not figure out the next step after this.

from google.oauth2 import service_account
from apiclient.http import call

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
SERVICE_ACCOUNT_FILE = 'credentials/credentials.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

Google's documentation does give you documentation around the API, but there is no sample code on how to invoke the methods or what to import within your Python code and what are the ways to invoke those methods.

How do you send a POST request with JSON data to an Cloud Function, with authorization through a service account?

**Edit A couple hours later I did some more digging and figured this out partially

from google.oauth2 import service_account

import googleapiclient.discovery
import json
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
SERVICE_ACCOUNT_FILE = 'credentials/credentials.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

cloudfunction = googleapiclient.discovery.build('cloudfunctions', 'v1', credentials=credentials)
#projects/{project_id}/locations/{location_id}/functions/{function_id}.
path='some project path'
data='some data in json that works when invoked through the console'
data=json.dumps(data)
a=cloudfunction.projects().locations().functions().call(name=path, body=data).execute()

I get another error now.

 Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'description': 'Invalid JSON payload received. Unknown name "": Root element must be a message.'}]}]">

I cant find any documentation on this. How should the JSON be formed?

making the json like {"message":{my actual payload}} doesn't work.


回答1:


The requested documentation can be found here.

The request body argument should be an object with the following form:

{ # Request for the `CallFunction` method.
    "data": "A String", # Input to be passed to the function.
}

The following modification on your code should work correctly:

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
SERVICE_ACCOUNT_FILE = 'credentials/credentials.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

cloudfunction = googleapiclient.discovery.build('cloudfunctions', 'v1', credentials=credentials)
path ="projects/your-project-name/locations/cloud-function-location/functions/name-of-cloud-function"
data = {"data": "A String"}
a=cloudfunction.projects().locations().functions().call(name=path, body=data).execute()

Notice that very limited traffic is allowed since there are limits to the API calls.



来源:https://stackoverflow.com/questions/58461277/invoking-a-google-cloud-function-from-a-django-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!