通过代理服务器拉取 PubSub 消息 - Python

问题描述 投票:0回答:1

我正在使用下面的脚本来拉取消息,我已经按照文档进行了开发,但遇到了错误

import json
from googleapiclient.discovery import build
from httplib2 import Http
import httplib2
from oauth2client.service_account import ServiceAccountCredentials

# Replace placeholders with your project ID, topic name, subscription name, and proxy details
project_id = 'v-acp'  # Replace with your GCP project ID
topic_name = 'd_ack'   # Replace with your Pub/Sub topic name
subscription_name = 'dpull'  # Replace with your desired subscription name
proxy_host = '192.173.10.2'  # Replace with your proxy server address
proxy_port = 8095  # Replace with your proxy server port

# Create credentials (replace with your own authentication method)
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'key.json',
    scopes=['https://www.googleapis.com/auth/pubsub']
)

# Configure HTTP connection with proxy
proxy_info = httplib2.ProxyInfo(proxy_type=httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL,
                                proxy_host=proxy_host,
                                proxy_port=proxy_port)
http = Http(proxy_info=proxy_info)

# Build the Pub/Sub API client
service = build('pubsub', 'v1', http=credentials.authorize(http))

# Pull messages from the subscription
def pull_messages():
    request = service.projects().subscriptions().pull(
        subscription=f'projects/{project_id}/subscriptions/{subscription_name}'
    )
    response = request.execute()
    if 'receivedMessages' in response:
        for message in response['receivedMessages']:
            # Process message data (message['message']['data']) and acknowledgment ID (message['ackId'])
            print(f"Received message: {message['message']['data']}")
            # Acknowledge message using service.projects().subscriptions().acknowledge()

# Call the pull_messages function to retrieve messages
pull_messages()

我收到以下错误

json 返回“您已向服务传递了无效参数 (argument=max_messages)。”。详细信息:“您向服务传递了无效参数 (argument=max_messages)。”

python proxy google-cloud-pubsub publish-subscribe http-proxy
1个回答
0
投票

我在正文中添加了 Max Message,它工作正常

request = service.projects().subscriptions().pull(
        subscription=f'projects/{project_id}/subscriptions/{subscription_name}',
        body={'maxMessages': 10}
    )
© www.soinside.com 2019 - 2024. All rights reserved.