英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论