通过解析 API 发送推送导致 401 未授权

Sending Push Through Parse API Results in 401 Unauthorized

我正在尝试通过 Parse 从我们的 python 后端向 iOS 移动应用程序发送推送通知。 iOS 端的所有内容都已配置,但是当我向 Parse API 发出请求时,我得到了 401 Unauthorized 响应。

headers = {'X-Parse-REST-API-Key': 'MY_API_KEY', 'X-Parse-Master-Key': 'MY_MASTER_KEY', 'Content-Type': 'content/json', 'X-Parse-Application-Id': 'MY_APPLICATION_ID'}

params = {'channels': ['general'], 'data': {'alert': 'Test push'}}

url = 'http://api.parse.com/1/push'

r = requests.post(url, params = params, headers = headers)

print r.status_code
>> 401
url = "https://api.parse.com:443/1/push"    
params = {'channels': ['general'], 'data': {'alert': 'Test push'}}
headers = {"Content-Type": "application/json",
           "X-Parse-Application-Id":"APP_ID",   
           "X-Parse-REST-API-Key":"API_KEY"}

r = requests.post(url, data = json.dumps(params), headers = headers)

r.status_code
>>>200

r.text
>>>u'{"result":true}\n'

在 Python 和 Parse.com 中使用 requests 函数而不是 httplib 需要什么:

  1. Url 需要带 https 的端口号。 (取自使用 httplib 的解析示例)
  2. Parse Rest API Docs 中所述,
  3. Headers POST 需要 Content-Type : application/json
  4. Params 必须作为请求传递 body 而不是 url 编码