我在项目 Project 1 中创建了一个主题,我在Google应用引擎上有一个应用程序,每分钟都会向此主题发布一条消息。
我在第二个项目( Project 2 )中有一台谷歌云计算机,它订阅了这个主题并接收了这些消息。
我没有在 Project 2 上授予对该计算机的任何访问权限,但即使没有访问权限,它也设法接收了这些消息。更准确地说,我没有写出与我创建的主题相关的特定权限。
我的问题是:
1-这是正常的吗?不应该 Project 2 上的机器出现"禁止访问错误"?
2-如何限制对某个主题的访问?
以下是我订阅部分的代码:
import httplib2
import base64
import pandas
import json
from apiclient import discovery
from oauth2client import client as oauth2client
from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.client import GoogleCredentials
def create_pubsub_client(http=None):
credentials = GoogleCredentials.get_application_default()
if not http:
http = httplib2.Http()
credentials.authorize(http)
return discovery.build('pubsub', 'v1', http=http)
client = create_pubsub_client()
# You can fetch multiple messages with a single API call.
batch_size = 1
subscription_str = 'projects/<myproject1>/subscriptions/testo'
# Create a POST body for the Pub/Sub request
body = {
# Setting ReturnImmediately to false instructs the API to wait
# to collect the message up to the size of MaxEvents, or until
# the timeout.
'returnImmediately': False,
'maxMessages': batch_size,
}
while True:
resp = client.projects().subscriptions().pull(
subscription=subscription_str, body=body).execute()
received_messages = resp.get('receivedMessages')
if received_messages is not None:
ack_ids = []
for received_message in received_messages:
pubsub_message = received_message.get('message')
if pubsub_message:
# Process messages
msg = base64.b64decode(str(pubsub_message.get('data')))
treatment(msg)
# Get the message's ack ID
ack_ids.append(received_message.get('ackId'))
# Create a POST body for the acknowledge request
ack_body = {'ackIds': ack_ids}
# Acknowledge the message.
client.projects().subscriptions().acknowledge(
subscription=subscription_str, body=ack_body).execute()
答案 0 :(得分:1)
Project 2中的计算机访问Project 1中的主题/订阅的能力完全取决于计算机的身份验证方式。如果通过对两个项目(例如您的开发人员帐户)具有权限的内容进行身份验证,那么您将能够访问项目1中主题的订阅。这是正常的。
如果要限制访问权限,请在项目1中创建service account,并设置主题和/或订阅的权限,以仅允许该服务帐户。您可以在Google Developers Console的Pub / Sub部分执行此操作。然后,只有通过该服务帐户进行身份验证的计算机才能访问它们。