繁体   English   中英

无法从项目 B 中的云 function 访问项目 A 中的云 Firestore - “403 权限缺失或不足”

[英]unable to access the cloud Firestore in Project A from cloud function in Project B - '403 Missing or insufficient permissions'

项目 A 中的 Cloud Firestore,项目 B 中的 Cloud Functions

我可以从“项目 B”的云 shell 访问“项目 A”的云 Firestore,但不能通过“项目 B”的云功能。 这是云function使用的代码

from google.cloud import firestore 
import firebase_admin
from firebase_admin import credentials

def function_a(request):

cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
    'projectId': 'project-a',
    })
db = firestore.client()
try:
    doc_ref=db.collection('COLLECTION_SAMPLE').document('documentA').set({"foo":"bar"})
except Exception as e:
    print("error::", e)
    
return "success"

项目 B 中的云函数使用服务帐户project-b@appspot.gserviceaccount.com服务帐户在项目 B 中具有角色( Cloud Datastore UserCloud Functions DeveloperEditor ),并在中具有( Cloud Datastore Import Export Admin )角色项目A。

我遇到403 Missing or insufficient permissions

  1. 将您的代码更改为
    db = firestore.Client(project='project-a')
  1. 找到您的 Cloud Function 的服务帐号。 正如您所提到的,通常应该是project-b@appspot.gserviceaccount.com 复制服务帐号 ID。

  2. 在项目project-a > IAM & Admin > IAM 的 GCP 控制台中,将步骤 2 中提到的服务帐户添加为其成员,并为其授予所需的角色,即 Cloud Datastore Viewer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM