请用python实现google service account认证的代码
时间: 2024-03-01 17:51:07 浏览: 10
好的,以下是使用Python实现Google Service Account认证的代码示例:
```python
import os
from google.oauth2 import service_account
from google.cloud import storage
# 读取Service Account的私钥文件路径
key_path = '/path/to/service_account.json'
# 创建Service Account对象
creds = None
if os.path.exists(key_path):
creds = service_account.Credentials.from_service_account_file(key_path)
# 使用Service Account对象访问Google Cloud Storage
if creds is not None:
storage_client = storage.Client(credentials=creds)
buckets = list(storage_client.list_buckets())
print(buckets)
else:
print('Service Account credentials not found!')
```
其中,`key_path`变量为Service Account的私钥文件路径,需要根据实际情况进行修改。如果私钥文件存在,则使用`service_account.Credentials.from_service_account_file()`方法创建Service Account对象,并使用该对象访问Google Cloud Storage服务。否则,输出提示信息表示Service Account凭据未找到。