Skip to content Skip to sidebar Skip to footer

Google Cloud Messaging Http Error 400: Bad Request

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) f

Solution 1:

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")

Solution 2:

Try using python-gcm. It can handle errors as well.

Solution 3:

Here is how I ended up solving it, but the above works as well.

defsendGCM(self, regid, email, entry_id, date_modified, kind):


    url = 'https://android.googleapis.com/gcm/send'
    apiKey = _MY_API_KEY
    myKey = "key=" + apiKey

    json_data = { "registration_id": regid, "data" : {
        "entry_id" : entry_id,
        "email": email,
        "date_modified": date_modified,
        "kind": kind,
        "reg_id": regid,
        },
    }

   ### Get regids
    registration_data = {
        "registration_ids": [regid],
    }

    headers = {'Content-Type': 'application/json', 'Authorization': myKey}
    data = urllib.urlencode(json_data)               
    req = urllib2.Request(url, data)
    req.add_header("Authorization", myKey)               

    f = urllib2.urlopen(req)
    response = f.read()
    f.close()

Post a Comment for "Google Cloud Messaging Http Error 400: Bad Request"