Python get_iam_policies 发布订阅

Python get_iam_policies PUBSUB

我正在尝试 运行 此代码并遇到此错误...知道我该如何解决这个问题吗?

 from google.cloud import pubsub_v1
 topic_id = 'start-instance-event'
 client = pubsub_v1.PublisherClient(credentials=credentials)
 topic_path = client.topic_path(project_id, topic_id)
 policy = client.get_iam_policy(request={"resource": topic_path})
 print("Policy for topic {}:".format(topic_path))
 for binding in policy.bindings:
    print("Role: {}, Members: {}".format(binding.role, binding.members))

错误:

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.PERMISSION_DENIED
    details = "User not authorized to perform this action."
    debug_error_string = "{"created":"@1604588362.110000000","description":"Error received from peer ipv6:[2a00:1450:4007:816::200a]:443","file":"src/core/lib/surface/call.cc","file_line":1062,"grpc_message":"User not authorized to perform this action.","grpc_status":7}"
>

The above exception was the direct cause of the following exception:

PermissionDenied                          Traceback (most recent call last)

由于权限错误,这似乎与权限问题有关。您应该尝试 Setting up authentication before to running your code. In this documentation 您会发现如何通过 IAM API.

控制访问
    from google.cloud import pubsub_v1
    
    # TODO(developer): Choose an existing topic.
    # project_id = "your-project-id"
    # topic_id = "your-topic-id"
    
    client = pubsub_v1.PublisherClient()
    topic_path = client.topic_path(project_id, topic_id)
    
    policy = client.get_iam_policy(request={"resource": topic_path})
    
    print("Policy for topic {}:".format(topic_path))
    for binding in policy.bindings:
        print("Role: {}, Members: {}".format(binding.role, binding.members))