2

I'm trying to send a request to parse.com's REST API. According to parse's documentation I need to put the App ID and API Key on the request.

I tried to do this using slumber but I keep getting Client Error 401: http://api.parse.com/1/installations/

What is the proper way to add headers to slumber? I tried following the docs http://slumber.readthedocs.org/en/latest/options.html#custom-session-objects but it seems to be outdated and even after some modifications, it still didn't work.

For reference, here is my code:

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()

EDIT: Instead of X-Parse-REST-API-Key, it's actually X-Parse-Master-Key

gerald
  • 57
  • 7

1 Answers1

1

I think best way to do this is to use a custom Authentication 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"))
Ilya Tikhonov
  • 1,399
  • 11
  • 7
  • Thanks for your reply. I tried to do as you said but I'm still getting 401. I'm sure that my headers should be correct because it works for curl in terminal. I'm curious to know if slumber is actually sending my headers. Is there a way to see the contents of the request slumber sends? – gerald Aug 21 '15 at 06:30
  • nvm that reply. I was using http when it's supposed to be https. however, it would still be nice to see what the requests look like for debugging purposes. – gerald Aug 21 '15 at 06:36
  • slumber is using requests library under the hood, use this to enable debug logging: http://stackoverflow.com/questions/10588644/how-can-i-see-the-entire-http-request-thats-being-sent-by-my-python-application – Ilya Tikhonov Aug 21 '15 at 13:40