Python 如何使用API密钥或OAuth客户端ID作为凭据将文件上载到Google平台?
我正在学习使用Python访问Google平台。我有API密钥和OAuth客户端ID的json文件。但是,官方网站只显示如何通过服务帐户使用Python 如何使用API密钥或OAuth客户端ID作为凭据将文件上载到Google平台?,python,google-cloud-platform,Python,Google Cloud Platform,我正在学习使用Python访问Google平台。我有API密钥和OAuth客户端ID的json文件。但是,官方网站只显示如何通过服务帐户使用 from google.cloud import storage def upload_blob(bucket_name, source_file_name, destination_blob_name): """Uploads a file to the bucket."""
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
# bucket_name = "your-bucket-name"
# source_file_name = "local/path/to/file"
# destination_blob_name = "storage-object-name"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(
"File {} uploaded to {}.".format(
source_file_name, destination_blob_name
)
)
由于某些公司的限制,我无法通过服务帐户访问。我去查看官方网站,但仍然不知道如何使用它们
有人能帮我解决这个问题吗?谢谢。要访问谷歌云API,您需要一个访问令牌。您有几种解决方案可以在不使用服务帐户的情况下生成它:
- 您可以使用OAuth客户端ID(从客户端用户帐户生成访问令牌)。用户帐户必须是Google帐户(gmail、play store、Workspace等)。但用户并不是每次都参与进来,而且您可能不想直接授权用户在资源上发送电子邮件
- 使用(我或与API Gateway(本文中描述的云端点ESPv2的托管版本,但原理和API规范定义相同)。在这种情况下,您可以使用API密钥,或来自Firebase Auth或Google Identity Platform(授权IdP联盟)的用户凭据。您可以在我的文章中描述的云函数、云运行或应用程序引擎上定义自己的服务,但也可以直接在API规范中包装Google API
- Use是一款全新的产品,目前仍处于预览阶段,没有UI。因此,到目前为止,他不是很容易使用,一开始可能很难使用。这一原则如下:
- 您的外部系统(或用户)拥有自己的OAuth2身份验证提供程序并生成标识令牌
- 此令牌被提交到工作负载联合池,工作负载联合池提取声明、验证授权并使用联合令牌进行应答
- 最后,请求IAM服务帐户凭据API从此凭据生成访问令牌
- 然后在请求中使用访问令牌