使用Golang通过HTTP触发云函数

huangapple go评论86阅读模式
英文:

Golang Trigger Cloud Function via http

问题

我尝试从一个云函数中使用 JSON Body 调用另一个云函数。

以下是一个示例代码:

// 云函数 URL
url := "https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME]"

// 请求体
requestBody := []byte(`{"name": "John"}`)

// 创建 HTTP 客户端
client := &http.Client{}

// 创建带有请求体的 POST 请求
request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
    panic(err)
}

// 发送请求并获取响应
response, err := client.Do(request)
if err != nil {
    panic(err)
}

// 打印响应体
var responseBody bytes.Buffer
_, err = responseBody.ReadFrom(response.Body)
if err != nil {
    panic(err)
}
fmt.Println(responseBody.String())

但是它返回 401 未授权错误。云函数的服务账号具有 Cloud Functions Invoker 角色和对云函数的访问权限。

英文:

I try to invoke a Cloud Function from Cloud Function with a JSON Body.

only a example code

// Cloud Function URL
url := "https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME]"

// Request body
requestBody := []byte(`{"name": "John"}`)

// Create HTTP client
client := &http.Client{}

// Create POST request with request body
request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
    panic(err)
}

// Send request and get response
response, err := client.Do(request)
if err != nil {
    panic(err)
}

// Print response body
var responseBody bytes.Buffer
_, err = responseBody.ReadFrom(response.Body)
if err != nil {
    panic(err)
}
fmt.Println(responseBody.String())

but it failing with 401 unautorized The service Account of the cloud Function have Cloud Functions Invoker Role and access to the Cloud Function.

答案1

得分: 1

你忘记添加授权头部了

// 云函数 URL
url := "https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME]"

// 请求体
requestBody := []byte(`{"name": "John"}`)

// 使用默认服务帐号凭据创建 HTTP 客户端
ctx := context.Background()
client, err := google.DefaultClient(ctx, cloudfunctions.CloudPlatformScope)
if err != nil {
    panic(err)
}

// 创建带有请求体的 POST 请求
request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
    panic(err)
}

// 使用默认服务帐号凭据对请求进行身份验证
token, err := google.DefaultTokenSource(ctx, cloudfunctions.CloudPlatformScope).Token()
if err != nil {
    panic(err)
}
request.Header.Set("Authorization", "Bearer "+token.AccessToken)

// 发送请求并获取响应
response, err := client.Do(request)
if err != nil {
    panic(err)
}

// 打印响应体
var responseBody bytes.Buffer
_, err = responseBody.ReadFrom(response.Body)
if err != nil {
    panic(err)
}
fmt.Println(responseBody.String())
英文:

You forgot to add the Authorization header

// Cloud Function URL
url := "https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME]"

// Request body
requestBody := []byte(`{"name": "John"}`)

// Create HTTP client with default service account credentials
ctx := context.Background()
client, err := google.DefaultClient(ctx, cloudfunctions.CloudPlatformScope)
if err != nil {
    panic(err)
}

// Create POST request with request body
request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
    panic(err)
}

// Authenticate request using default service account credentials
token, err := google.DefaultTokenSource(ctx, cloudfunctions.CloudPlatformScope).Token()
if err != nil {
    panic(err)
}
request.Header.Set("Authorization", "Bearer "+token.AccessToken)

// Send request and get response
response, err := client.Do(request)
if err != nil {
    panic(err)
}

// Print response body
var responseBody bytes.Buffer
_, err = responseBody.ReadFrom(response.Body)
if err != nil {
    panic(err)
}
fmt.Println(responseBody.String())


</details>



huangapple
  • 本文由 发表于 2023年4月5日 23:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75941117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定