5

これは私の見解です:

@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ているのは、上記の行だけです。

また、それが機能する他のいくつかの URL をテストしてみました!

何がうまくいかないのですか?

4

1 に答える 1

2

Google から 401 応答が返ってきた場合は、認証に失敗しています。文書化された承認のトラブルシューティング ページには、このような失敗の次の理由が記載されています。

  • 認証ヘッダーが見つからないか、構文が無効です。
  • キーとして送信されたプロジェクト番号が無効です。
  • キーは有効ですが、GCM サービスが無効になっています。
  • サーバー キー IP でホワイトリストに登録されていないサーバーからの要求。

キーが有効で有効であること、および IP アドレスが正しくホワイトリストに登録されていることを確認してください。IP アドレスは、Django サーバーが API に接続するときに Google サービスが認識するアドレスです。

于 2013-11-08T12:21:46.457 に答える