Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to send a POST request in GCM using Django

This is my view:

@csrf_exempt
def send_notification(request):
    message = {"name": "xxxxx","email": "[email protected]"}
    registration_ids = ['xxxxxxxxxxxxxx']
    post_data = {"data":message,"registration_ids":registration_ids}
    headers = {'Content-Type': 'application/json', 'Authorization':'key=xxxxxx'}
    import requests
    post_response = requests.post(url='https://android.googleapis.com/gcm/send', data=simplejson.dumps(post_data), headers=headers)  
    print post_response
    print simplejson.dumps([post_response.headers, post_response.text])

    to_json = {'status':str(post_response)}
    return HttpResponse(simplejson.dumps(to_json), mimetype='application/json')

Server Log:

[error] <Response [401]>

Output:

TypeError at /send_notification/

CaseInsensitiveDict(
    {
        "alternate-protocol": "443:quic",
        "x-xss-protection": "1; mode=block",
        "x-content-type-options": "nosniff",
        "transfer-encoding": "chunked",
        "expires": "Fri, 08 Nov 2013 10:29:45 GMT",
        "server": "GSE",
        "cache-control": "private, max-age=0",
        "date": "Fri, 08 Nov 2013 10:29:45 GMT",
        "x-frame-options": "SAMEORIGIN",
        "content-type": "text/html; charset=UTF-8"
   }) is not JSON serializable

I'm using requests which gives me just the above line.

I also tried testing some other urls where it worked!

What could be going wrong?

like image 249
Nullify Avatar asked Nov 02 '22 11:11

Nullify


1 Answers

If you are getting a 401 response from Google, authorization failed. The documented authorization troubleshooting page states the following reasons for such a failure:

  • Authorization header missing or with invalid syntax.
  • Invalid project number sent as key.
  • Key valid but with GCM service disabled.
  • Request originated from a server not whitelisted in the Server Key IPs.

Make sure that your key is valid, enabled, and that your IP address(es) are correctly whitelisted. The IP address is the address the Google services see when your Django server contacts the API.

like image 191
Martijn Pieters Avatar answered Nov 04 '22 10:11

Martijn Pieters