Google云消息传递HTTP错误400:错误的请求 [英] Google Cloud Messaging HTTP Error 400: Bad Request
本文介绍了Google云消息传递HTTP错误400:错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
但是,我无法通过Google App Engine向GCM发送任何内容。
regId =APA91b ...
json_data = {collapse_key:Food-Promo,data:{
Category:FOOD,
Type:VEG,
},registration_ids :[regId],
}
url ='https://android.googleapis.com/gcm/send'
apiKey =AI ...
myKey =key =+ apiKey
$ b $ header = {'Content-Type':'application / json ','Authorization':myKey}
data = urllib.urlencode(json_data)
data2 = {title:title}
data3 = urllib.urlencode(data2)
req = urllib2.Request(url,data,headers)
$ bf = urllib2.urlopen(req)
response = f.read()
f .close()
logging.debug(*** !!!!!!! WriteEntry TEST ----- Response:+ response)
这里是我收到的错误。
Traceback(最近一次调用最后一次):
文件/ base / python_runtime / python_lib / versions / 1 / google / appengine / ext / webapp / _webapp25 .py,第703行,在__call__
handler.post(* groups)
文件/base/data/home/apps/s~journaltestza/26.360625174851783344/main.py,第213行,in post
f = urllib2.urlopen(req)
在urlopen
中的文件/base/python_runtime/python_dist/lib/python2.5/urllib2.py,第124行返回_opener.open( url,data)
打开
的文件/base/python_runtime/python_dist/lib/python2.5/urllib2.py,第387行response = meth(req,response)
File /base/python_runtime/python_dist/lib/python2.5/urllib2.py,第498行,在http_response
'http',请求,响应,代码,味精,hdrs)
文件/ base /python_runtime/python_dist/lib/python2.5/urllib2.py,第425行,错误
返回self._call_chain(* args)
文件/ base / python_runtime / python_dist / lib / python2。 5 / urllib2.py,第360行,在_call_chain
中result = func(* args)
在http_error_default
中的文件/base/python_runtime/python_dist/lib/python2.5/urllib2.py,第506行,引发HTTPError(req.get_full_url(),code ,msg,hdrs,fp)
HTTPError:HTTP错误400:错误请求
谢谢!
解决方案data2和data3用于什么目的?您发布的数据不是正确的json,所以您需要使用json.dumps(data)。代码应该是这样的:
json_data = {collapse_key:Food-Promo,data:{
Category:FOOD,
Type:VEG,
} registration_ids:[regId],
}
url ='https://android.googleapis.com/gcm/send'
apiKey =AI。 ..
myKey =key =+ apiKey
data = json.dumps(json_data)
headers = {'Content-Type':'application / json','Authorization': myKey}
req = urllib2.Request(url,data,headers)
f = urllib2.urlopen(req)
response = json.loads(f.read())
reply = {} $ b $如果response ['failure'] == 0:
reply ['error'] ='0'
else:
response ['error'] =' 1'
return HttpResponse(json.dumps(reply),mimetype =application / javascript)
I am trying to send a message through GCM (Google Cloud Messaging). I have registered through Google APIs, I can send a regID to my website (which is a Google App Engine Backend) from multiple Android test phones. However, I can't send anything to GCM from Google App Engine. Here is what I am trying to use.
regId = "APA91b..." json_data = {"collapse_key" : "Food-Promo", "data" : { "Category" : "FOOD", "Type": "VEG", }, "registration_ids": [regId], } url = 'https://android.googleapis.com/gcm/send' apiKey = "AI..." myKey = "key=" + apiKey headers = {'Content-Type': 'application/json', 'Authorization': myKey} data = urllib.urlencode(json_data) data2 = {"title": title} data3 = urllib.urlencode(data2) req = urllib2.Request(url, data, headers) f = urllib2.urlopen(req) response = f.read() f.close() logging.debug("***!!!!!!!WriteEntry TEST ----- Response: " + response)
And here is the error that I am receiving.
Traceback (most recent call last): File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__ handler.post(*groups) File "/base/data/home/apps/s~journaltestza/26.360625174851783344/main.py", line 213, in post f = urllib2.urlopen(req) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen return _opener.open(url, data) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 387, in open response = meth(req, response) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 498, in http_response 'http', request, response, code, msg, hdrs) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 425, in error return self._call_chain(*args) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain result = func(*args) File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 506, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 400: Bad Request
Thanks!
解决方案What are data2 and data3 used for ? The data you are posting was not proper json so you need to use json.dumps(data).Code should be like this :
json_data = {"collapse_key" : "Food-Promo", "data" : { "Category" : "FOOD", "Type": "VEG", }, "registration_ids": [regId], } url = 'https://android.googleapis.com/gcm/send' apiKey = "AI..." myKey = "key=" + apiKey data = json.dumps(json_data) headers = {'Content-Type': 'application/json', 'Authorization': myKey} req = urllib2.Request(url, data, headers) f = urllib2.urlopen(req) response = json.loads(f.read()) reply = {} if response ['failure'] == 0: reply['error'] = '0' else: response ['error'] = '1' return HttpResponse(json.dumps(reply), mimetype="application/javascript")
这篇关于Google云消息传递HTTP错误400:错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文