从Django视图调用Google Cloud函数 [英] Invoking a Google Cloud Function from a Django View

查看:67
本文介绍了从Django视图调用Google Cloud函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了可以通过HTTP调用的Google Cloud函数.该功能的访问仅限于服务帐户.

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.

如果我有一个Django View,它应该调用此函数并期望得到响应?

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

这是我尝试过的

1)在启动Django之前,我先设置了环境变量

1) Before starting Django I set the environment variable

export GOOGLE_APPLICATION_CREDENTIALS

2)我尝试使用独立代码调用该函数,但很快意识到这没有用,因为在此之后我无法弄清楚下一步.

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的文档确实为您提供了有关API的文档,但是没有关于如何调用方法或在Python代码中导入内容以及调用这些方法的方式的示例代码.

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.

如何通过服务帐户授权将带有JSON数据的POST请求发送到Cloud Function?

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.'}]}]">

我找不到与此有关的任何文档.JSON应该如何形成?

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

使json像 {"message":{我的实际有效载荷}} 一样无效.

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

推荐答案

可以找到所请求的文档

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()

请注意,由于API调用存在限制,因此允许的流量非常有限

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

这篇关于从Django视图调用Google Cloud函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆