【发布时间】:2013-11-20 07:11:12
【问题描述】:
这是我的看法:
@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,这给了我上面的行。
我还尝试测试其他一些有效的网址!
可能出了什么问题?
【问题讨论】:
-
正确,因此 API 会为您提供 401 错误响应。查看该响应的标题和正文以获取线索。
dumps([post_response.headers, post_response.text])说什么? -
另外,您阅读文档中的401 error troubleshooting section了吗?
-
对,但这至少告诉我们标题不包含错误反馈。删除标题,只返回
post_response.text。 -
@KousikChowdhury: np;所以故障排除页面是正确的:请求来自未在服务器密钥 IP 中列入白名单的服务器
-
@KousikChowdhury:我已经将其发布为答案,供未来访问该问题的人使用。
标签: python django google-cloud-messaging python-requests