函数 service_account.Credentials.from_service_account_info() 不工作
Function service_account.Credentials.from_service_account_info() not working
我正在编写一个基于 GCP 服务的应用程序,我需要访问一个外部项目。我将我需要访问的其他项目的身份验证文件信息存储在我的 Firestore 数据库中。我阅读 this documentation 并尝试应用它,但我的代码不起作用。正如文档所说,我传递给身份验证方法的是字典 [str, str]。
这是我的代码:
from googleapiclient import discovery
from google.oauth2 import service_account
from google.cloud import firestore
project_id = body['project_id']
user = body['user']
snap_id = body['snapshot_id']
debuggee_id = body['debuggee_id']
db = firestore.Client()
ref = db.collection(u'users').document(user).collection(u'projects').document(project_id)
if ref.get().exists:
service_account_info = ref.get().to_dict()
else:
return None, 411
credentials = service_account.Credentials.from_service_account_info(
service_account_info,
scopes=['https://www.googleapis.com/auth/cloud-platform'])
service = discovery.build('clouddebugger', 'v2', credentials=credentials)
body 只是一个字典,包含了其他项目的所有信息。我不明白的是为什么这不起作用,而是使用方法 from_service_account_file 它起作用了。
以下代码将向该方法提供与前面代码相同的信息,但在 json 文件而不是字典中。也许元素的顺序不同,但我认为完全没有关系。
credentials = service_account.Credentials.from_service_account_file(
[PATH_TO_PROJECT_KEY_FILE],
scopes=['https://www.googleapis.com/auth/cloud-platform'])
你能告诉我方法 from_service_account_info 做错了什么吗?
问题已解决。当我发布问题时,我从 GCP Firestore 控制台手动插入了有关其他项目的所有信息。然后我编写代码使其自动运行并且它起作用了。老实说,我不知道为什么它以前没有用,Firestore 中的信息和格式都是一样的。
我正在编写一个基于 GCP 服务的应用程序,我需要访问一个外部项目。我将我需要访问的其他项目的身份验证文件信息存储在我的 Firestore 数据库中。我阅读 this documentation 并尝试应用它,但我的代码不起作用。正如文档所说,我传递给身份验证方法的是字典 [str, str]。 这是我的代码:
from googleapiclient import discovery
from google.oauth2 import service_account
from google.cloud import firestore
project_id = body['project_id']
user = body['user']
snap_id = body['snapshot_id']
debuggee_id = body['debuggee_id']
db = firestore.Client()
ref = db.collection(u'users').document(user).collection(u'projects').document(project_id)
if ref.get().exists:
service_account_info = ref.get().to_dict()
else:
return None, 411
credentials = service_account.Credentials.from_service_account_info(
service_account_info,
scopes=['https://www.googleapis.com/auth/cloud-platform'])
service = discovery.build('clouddebugger', 'v2', credentials=credentials)
body 只是一个字典,包含了其他项目的所有信息。我不明白的是为什么这不起作用,而是使用方法 from_service_account_file 它起作用了。 以下代码将向该方法提供与前面代码相同的信息,但在 json 文件而不是字典中。也许元素的顺序不同,但我认为完全没有关系。
credentials = service_account.Credentials.from_service_account_file(
[PATH_TO_PROJECT_KEY_FILE],
scopes=['https://www.googleapis.com/auth/cloud-platform'])
你能告诉我方法 from_service_account_info 做错了什么吗?
问题已解决。当我发布问题时,我从 GCP Firestore 控制台手动插入了有关其他项目的所有信息。然后我编写代码使其自动运行并且它起作用了。老实说,我不知道为什么它以前没有用,Firestore 中的信息和格式都是一样的。