Created
September 21, 2012 23:08
-
-
Save evanhammer/3764432 to your computer and use it in GitHub Desktop.
Python Examples of TaskRabbit Authentication and Access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using the py-oauth2 library | |
# http://github.com/liluo/py-oauth2 | |
import oauth2 | |
import json | |
SITE = "https://taskrabbitdev.com" | |
AUTHORIZE_URL = "https://taskrabbitdev.com/api/authorize" | |
TOKEN_URL = "https://taskrabbitdev.com/api/oauth/token" | |
HEADER_FORMAT = "OAuth %s" | |
APPLICATION_HEADER = "X-Client-Application" | |
CONTENT_TYPE = "Content-Type" | |
APPLICATION_JSON = "application_json" | |
TASKS_PATH = "/api/v1/tasks" | |
TASK_RABBIT_USER_CODE = <PULLED FROM AUTHORIZATION URL RESPONSE MANUALLY> | |
# Setup Access Token | |
client = oauth2.Client( | |
settings.TASK_RABBIT_KEY, | |
settings.TASK_RABBIT_SECRET, | |
site=SITE, | |
authorize_url=AUTHORIZE_URL, | |
token_url=TOKEN_URL, | |
header_format=HEADER_FORMAT) | |
access_token = client.auth_code.get_token( | |
TASK_RABBIT_USER_CODE, | |
redirect_uri="") | |
# Reads Tasks successfully | |
headers = {APPLICATION_HEADER: settings.TASK_RABBIT_SECRET} | |
response = access_token.get(TASKS_PATH, headers=headers) | |
print response.parsed | |
# Writes Task unsuccessfully | |
data_dict = {"task": | |
{"name": "Test Task 2", "named_price": "20", "description": "this is the wicked description."}} | |
headers = {APPLICATION_HEADER: settings.TASK_RABBIT_SECRET, CONTENT_TYPE: APPLICATION_JSON} | |
json_data = json.dumps(data_dict) | |
response = self._access_token.post(TASKS_PATH, body=json_data, headers=headers) | |
print response.parsed | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think there might be an issue with the headers. You are passing the client secret, but you need to pass the user authtoken and the client app id (public key). Additionally, I do not see where the authtoken is being generated from OmniAuth.
Headers
Authorization: OAuth [user token here, not response code]
X-Client-Application: [public API key here]
This example might be useful.