Skip to content

Instantly share code, notes, and snippets.

@paul-english
Created January 4, 2017 16:26
Show Gist options
  • Save paul-english/8dec29a053aba2519d7c40191e487c53 to your computer and use it in GitHub Desktop.
Save paul-english/8dec29a053aba2519d7c40191e487c53 to your computer and use it in GitHub Desktop.
Asana Python Social Auth
import json
import urllib.error
import urllib.parse
import urllib.request
from social.backends.oauth import BaseOAuth2
class AsanaOAuth2(BaseOAuth2):
"""Asana OAuth authentication backend"""
name = 'asana'
AUTHORIZATION_URL = 'https://app.asana.com/-/oauth_authorize'
ACCESS_TOKEN_URL = 'https://app.asana.com/-/oauth_token'
ACCESS_TOKEN_METHOD = 'POST'
REDIRECT_STATE = False
REFRESH_TOKEN_URL = 'https://app.asana.com/-/oauth_token'
REFRESH_TOKEN_METHOD = 'POST'
EXTRA_DATA = [
('expires_in', 'expires_in',),
('token_type', 'token_type',),
('refresh_token', 'refresh_token',),
('data', 'data',),
]
# from social.apps.django_app.utils import load_strategy
# strategy = load_strategy()
# sa = user.social_auth.get(provider='asana')
# sa.refresh_token(strategy)
# REQUIRES_EMAIL_VALIDATION = True
# EXTRA_DATA = []
# RESPONSE_TYPE = ''
def get_user_details(self, response):
data = response.get('data')
return {
'email': data.get('email'),
'id': data.get('id'),
'name': data.get('name'),
'photo': data.get('photo'),
'workspaces': data.get('workspaces')
}
def user_data(self, access_token, *args, **kwargs):
url = 'https://app.asana.com/api/1.0/users/me'
headers = {"Authorization": "Bearer %s" % access_token}
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
return json.load(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment