我在同一个GCP项目上有2个Google Cloud Function 1 Gen,R和C.
R是一个私有函数,其"只允许内部流量"标志设置为真.在权限选项卡中,我将调用者角色分配给服务帐户SA.
C是一个公共函数,它将所有出口流量重定向到无服务器VCP连接器.
不出所料,如果我try 从浏览器调用R,我会收到404Not Found错误.
在他的代码中,C使用SA json密钥(保存在秘密管理器上)来获取身份验证令牌,然后向R发出HTTP请求,使用检索到的持有者令牌设置身份验证头.
这里是R代码:
def hello_world(request):
return f'Hello World!'
下面是C代码:
import requests
import functions_framework
import google.auth
import google.auth.transport.requests
from flask import Request
from typing import Tuple
from google.oauth2 import service_account
import os, json
def get_token() -> str:
service_account_key = json.loads(os.getenv('SA'))
credentials = service_account.Credentials.from_service_account_info(
service_account_key,
scopes=['https://www.googleapis.com/auth/cloud-platform']
)
auth_req = google.auth.transport.requests.Request()
credentials.refresh(auth_req)
return credentials.token
@functions_framework.http
def test(req: Request) -> Tuple[str, int]:
url = "..."
headers = {
"Authentication": f"Bearer {get_token()}"
}
response = requests.get(url, headers=headers)
return response.text, response.status_code
我希望C可以调用R,但我收到了403禁止错误.
C调用R的唯一方法是将R公之于众(我已经try 过了,而且很有效).
这就是例外行为吗?我错过了什么吗?