【发布时间】:2015-05-22 09:49:07
【问题描述】:
我正在尝试使用 Python slimXMPP 通过 Google Cloud Messaging 发送消息。我尝试按照GCM docs 中的示例进行操作。但是,当我调用 send_command 时,我收到“InvalidJson:MissingPayload”错误响应 (400)。我在这里想念什么?以下是我使用的代码。
def random_id():
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))
class GcmNotifier(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
super(GcmNotifier, self).__init__(jid, password)
self.add_event_handler('message', self.on_message_received)
def send_gcm_message(self, message):
body = '<gcm xmlns:"google:mobile:data">%s</gcm>' % json.dumps(message)
print(body)
self.send_message(mto='', mbody=body)
def on_message_received(self, message):
print(message)
def send_command(self, recipient):
self.send_gcm_message({
'to': recipient,
'message_id': random_id(),
'data':
{
'hello': 'world'
}
})
xmpp = GcmNotifier(GCM_SENDER_ID + '@gcm.googleapis.com', GCM_API_KEY)
if xmpp.connect((GCM_SERVER, GCM_PORT), use_ssl=True):
xmpp.process(block=False)
这是我收到的错误:
<message to="REDACTED@gcm.googleapis.com/475DBA7C" type="error" xml:lang="en"><body>&lt;gcm xmlns:&quot;google:mobile:data&quot;&gt;{&quot;to&quot;: &quot;REDACTED&quot;, &quot;data&quot;: {&quot;hello&quot;: &quot;world&quot;}, &quot;message_id&quot;: &quot;ZGDZ9QTD&quot;}&lt;/gcm&gt;</body><error code="400" type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">InvalidJson : MissingPayload</text></error></message>
【问题讨论】:
标签: python xmpp google-cloud-messaging