GCM无效JSON缺失负载 [英] GCM Invalid JSON Missing Payload
问题描述
我正尝试使用Python sleekXMPP通过Google Cloud Messaging发送消息。我试图遵循 GCM文档中的示例。但是,当我调用 send_command
时,出现InvalidJson:MissingPayload错误响应(400)。我在这里错过了什么?以下是我使用的代码。
def random_id():
return''.join(random。 (8)中_的选择(string.ascii_uppercase + string.digits))
class GcmNotifier(sleekxmpp.ClientXMPP):
def __init __(self,jid,password ):
super(GcmNotifier,self).__ init __(jid,password)
self.add_event_handler('message',self.on_message_received)
$ b $ 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)
$ b $ def send_command(self,recipient) :
self.send_gcm_message({$ b $'到':收件人,$ b $'message_id':random_id(),
'data':
{
' hello':'w (GCM_SERVER,orld'
}
})
xmpp = GcmNotifier(GCM_SENDER_ID +'@ gcm.googleapis.com',GCM_API_KEY)
如果xmpp.connect GCM_PORT),use_ssl = True):
xmpp.process(block = False)
是我收到的错误:
< message to =REDACTED@gcm.googleapis.com/475DBA7Ctype =errorxml :lang =en>< body>& lt; gcm xmlns:& quot; google:mobile:data& quot;& gt; {& quot; to& quot;& quot; REDACTED& amp & quot;& quot; data& quot;:{& quot; hello& quot;:& quot; world& quot;}," message_id& quot;:& quot; ZGDZ9QTD& quot; }< / gcm& gt;< / body>< error code =400type =modify>< bad-request xmlns =urn:ietf:params:xml:ns:xmpp- stanzas/>< / text>< / x>< / error>< / message>
它原来SleekXMPP自动将我的消息封装在< body />
标签中,这不是GCM服务器预期的消息格式。
class Gcm(ElementBase):
namespace我通过定义自己的节来解决问题。 ='google:mobile:data'
name ='gcm'
plugin_attrib ='gcm'
interfaces = set('gcm')
sub_interfaces = interfaces
class GcmMessage(ElementBase):
namespace =''
name ='message'
interfaces = set('gcm')
sub_interfaces = interfaces
子项目=(Gcm,)
register_stanza_plugin(GcmMessage,Gcm)
然后通过发送如下消息:
def send_gcm_message(self,message):
msg = GcmMessage()
msg ['gcm']。xml.text = xml.sax.saxutils.escape(json.dumps(message,ensure_ascii = False))
self.send(msg)
I am trying to send messages through Google Cloud Messaging, using Python sleekXMPP. I tried to follow the sample in the GCM docs. However, I am getting an "InvalidJson : MissingPayload" error response (400) when I call send_command
. What am I missing here? The following is the code that I use.
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)
This is the error that I receive:
<message to="REDACTED@gcm.googleapis.com/475DBA7C" type="error" xml:lang="en"><body><gcm xmlns:"google:mobile:data">{"to": "REDACTED", "data": {"hello": "world"}, "message_id": "ZGDZ9QTD"}</gcm></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>
It turns out SleekXMPP was automatically enclosing my message in <body />
tags, which is not the expected message format by the GCM server. I ended up solving the problem by defining my own stanzas, like this:
class Gcm(ElementBase):
namespace = 'google:mobile:data'
name = 'gcm'
plugin_attrib = 'gcm'
interfaces = set('gcm')
sub_interfaces = interfaces
class GcmMessage(ElementBase):
namespace = ''
name = 'message'
interfaces = set('gcm')
sub_interfaces = interfaces
subitem = (Gcm,)
register_stanza_plugin(GcmMessage, Gcm)
and then by sending the message like this:
def send_gcm_message(self, message):
msg = GcmMessage()
msg['gcm'].xml.text = xml.sax.saxutils.escape(json.dumps(message, ensure_ascii=False))
self.send(msg)
这篇关于GCM无效JSON缺失负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!