2012-07-28 25 views
10

Próbuję wysłać wiadomość przez GCM (Google Cloud Messaging). Zarejestrowałem się za pomocą Google API, mogę wysłać regID do mojej strony internetowej (która jest mechanizmem Google App Engine) z wielu telefonów testowych z Androidem. Nie mogę jednak wysłać niczego do GCM z Google App Engine. Oto, co próbuję użyć.Google Cloud Messaging HTTP Błąd 400: Bad Request

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) 

Tutaj pojawia się błąd, który otrzymuję.

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 

Dzięki!

Odpowiedz

9

Do czego służą dane data2 i data3? Dane Piszesz nie wypada json więc trzeba użyć json.dumps (dane) .Code powinno być tak:

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

Spróbuj użyć python-gcm. Może również obsługiwać błędy.

0

Oto jak doszedłem do rozwiązania, ale powyższe działa również.

def sendGCM(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() 
+0

Wygląda na to, że używasz kombinacji formatów json i zwykłego tekstu. Wybierz jedno. http://developer.android.com/guide/google/gcm/gcm.html#server – Albert

Powiązane problemy