[英]Not able to send a POST request in GCM using Django
这是我的看法:
@csrf_exempt
def send_notification(request):
message = {"name": "xxxxx","email": "xxxxxx@gmail.com"}
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')
服务器日志:
[error] <Response [401]>
输出:
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
我正在使用的requests
只给出了上述内容。
我也试过测试其他工作的网址!
怎么可能出错?
如果您收到来自Google的401回复,则授权失败。 文档化的授权疑难解答页面说明了导致此类失败的以下原因:
- 授权标头丢失或语法无效。
- 作为密钥发送的项目编号无效。
- 密钥有效但GCM服务已禁用。
- 请求源自服务器密钥IP中未列入白名单的服务器。
确保您的密钥有效,已启用,并且您的IP地址已正确列入白名单。 IP地址是当Django服务器与API联系时Google服务看到的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.