Stackdriver Google Python API 拒绝访问

Stackdriver Google Python API Access Denied

尝试使用 Google Cloud Python3 API 客户端创建 sink 时出现错误:

RetryError: GaxError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.PERMISSION_DENIED, The caller does not have permission)>)

我使用的代码是这个:

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path_to_json_secrets.json'

from google.cloud.bigquery.client import Client as bqClient
bqclient = bqClient()
ds = bqclient.dataset('dataset_name')

print(ds.access_grants)
[]

ds.delete()
ds.create()

print(ds.access_grants)
[<AccessGrant: role=WRITER, specialGroup=projectWriters>,
 <AccessGrant: role=OWNER, specialGroup=projectOwners>,
 <AccessGrant: role=OWNER, userByEmail=id_1@id_2.iam.gserviceaccount.com>,
 <AccessGrant: role=READER, specialGroup=projectReaders>]

from google.cloud.logging.client import Client as lClient
lclient = lClient()
dest = 'bigquery.googleapis.com%s' %(ds.path)
sink = lclient.sink('sink_test', filter_='jsonPayload.project=project_name', destination=dest)
sink.create()

不太明白为什么会这样。当我使用 lclient.log_struct() 时,我可以看到日志到达日志记录控制台,因此我可以访问 Stackdriver 日志记录。

这个设置有没有错误?

提前致谢。

创建接收器需要不同于写入日志条目的权限。默认情况下,服务帐户被授予项目编辑器(而非所有者),它没有创建接收器的权限。

查看所需权限列表in the access control docs

确保您使用的服务帐户具有 logging.sinks.create 权限。最简单的方法是将服务帐户从 Editor 切换为 Owner,但最好添加 Logs Editor Role,这样您就可以授予它所需的权限。