2017-04-16 25 views
0

Firebase経由で特定のクライアントにメッセージを送信しようとしています。これは私の現在の(テスト)コードです:Firebaseによって返され、次のエラー、生成Python上の位置0の予期しないトークンENDOF FILEBASEへのPOST要求

import json 
import requests 
import urllib 

def send_message(): 
    server = "https://fcm.googleapis.com/fcm/send" 
    api_key = "xxx" 
    user_token = "xxx" 

    headers = {'Content-Type': 'application/json', 'Authorization': 'key=' + api_key} 

    data = {"type": "dataUpdate"} 
    payload = {"data": data, "to": user_token} 
    payload = json.dumps(payload) 

    res = requests.post(server, headers=headers, json=payload) 

    return res 

:Firebaseに送ら

JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0. 

次JSONは、私には正しいようだ:

{ 
    "data": { 
     "type":"dataUpdate" 
    }, 
    "to":"xxx" 
} 

は、Firebase documentationで記述された形式です。 Firebaseが与えられたデータを受け入れない理由は何ですか?

答えて

4

requests.post()のパラメータとしてjson=payloadを使用する場合、ヘッダーに'Content-Type': 'application/json'を指定する必要はありません。パラメータが辞書として​​をすべきときにまた、あなたは文字列を渡している(すなわちjson.dumps()は不要)

はこれを試してみてくださいません:

def send_message(): 
    server = "https://fcm.googleapis.com/fcm/send" 
    api_key = "xxx" 
    user_token = "xxx" 

    headers = {'Authorization': 'key=' + api_key} 

    data = {"type": "dataUpdate"} 
    payload = {"data": data, "to": user_token} 

    res = requests.post(server, headers=headers, json=payload) 

    return res 
+0

このアプローチは動作します。どうもありがとうございました。 – CPUFry

+0

@ OpenUserX03 content-type:application/jsonヘッダーを設定すると要求が失敗するのはなぜですか?そのようなことが間違って起こる可能性があるので、私は心配しています。 –

+0

@ PubuduDodangoda 'json'パラメータを指定するだけでは不必要です – OpenUserX03

関連する問題