如何让headers睡在python?
How to add headers to slumber in python?
我正在尝试向 parse.com 的 REST API 发送请求。根据 parse 的文档,我需要将 App ID 和 API Key 放在请求中。
我尝试使用睡眠来做到这一点,但我不断
客户端错误 401:http://api.parse.com/1/installations/
增加headers睡眠的正确方法是什么?我尝试按照文档 http://slumber.readthedocs.org/en/latest/options.html#custom-session-objects 进行操作,但它似乎已经过时,即使经过一些修改,它仍然没有用。
供参考,这是我的代码:
session = requests.Session()
session.headers = {"X-Parse-Application-Id": APPLICATION_ID, "X-Parse-REST-API-Key": API_KEY}
api = slumber.API("http://api.parse.com/1/", session=session)
api.installations.get()
编辑:
而不是 X-Parse-REST-API-Key,它实际上是 X-Parse-Master-Key
我认为最好的方法是使用自定义身份验证 class http://slumber.readthedocs.org/en/latest/options.html#specify-authentication:
import slumber
from requests.auth import AuthBase
class ParseAuth(AuthBase):
def __init__(self, app_id, api_key):
self.app_id = app_id
self.api_key = api_key
def __call__(self, r):
r.headers['X-Parse-Application-Id'] = self.app_id
r.headers['X-Parse-REST-API-Key'] = self.api_key
return r
api = slumber.API("http://api.parse.com/1/", auth=ParseAuth("my_app_id", "my_api_key"))
我正在尝试向 parse.com 的 REST API 发送请求。根据 parse 的文档,我需要将 App ID 和 API Key 放在请求中。
我尝试使用睡眠来做到这一点,但我不断 客户端错误 401:http://api.parse.com/1/installations/
增加headers睡眠的正确方法是什么?我尝试按照文档 http://slumber.readthedocs.org/en/latest/options.html#custom-session-objects 进行操作,但它似乎已经过时,即使经过一些修改,它仍然没有用。
供参考,这是我的代码:
session = requests.Session()
session.headers = {"X-Parse-Application-Id": APPLICATION_ID, "X-Parse-REST-API-Key": API_KEY}
api = slumber.API("http://api.parse.com/1/", session=session)
api.installations.get()
编辑: 而不是 X-Parse-REST-API-Key,它实际上是 X-Parse-Master-Key
我认为最好的方法是使用自定义身份验证 class http://slumber.readthedocs.org/en/latest/options.html#specify-authentication:
import slumber
from requests.auth import AuthBase
class ParseAuth(AuthBase):
def __init__(self, app_id, api_key):
self.app_id = app_id
self.api_key = api_key
def __call__(self, r):
r.headers['X-Parse-Application-Id'] = self.app_id
r.headers['X-Parse-REST-API-Key'] = self.api_key
return r
api = slumber.API("http://api.parse.com/1/", auth=ParseAuth("my_app_id", "my_api_key"))