Last active
August 29, 2015 14:06
-
-
Save dzerrenner/491be7fd84156ddc9183 to your computer and use it in GitHub Desktop.
python-social-auth backend for battle.net oauth
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
from django.shortcuts import render_to_response | |
from social.backends.oauth import BaseOAuth2 | |
from social.pipeline.partial import partial | |
@partial | |
def pick_character_name(backend, details, response, is_new=False, *args, **kwargs): | |
if backend.name == 'battlenet-oauth2' and is_new: | |
data = backend.strategy.request_data() | |
if data.get('character_name') is None: | |
# New user and didn't pick a character name yet, so we render | |
# and send a form to pick one. The form must do a POST/GET | |
# request to the same URL (/complete/battlenet/). In this | |
# example we expect the user option under the key: | |
# character_name | |
# extrect the character list for display on the frontend. in this case only the name field | |
# is extrected, feel free to include more | |
char_list = [c['name'] for c in backend.get_characters(response.get('access_token')) | |
if 'guild' in c and c['guild'] == 'Oakheart'] # ToDo: make guild name a parameter | |
return render_to_response('pick_character_form.html', {'charlist': char_list, }) | |
else: | |
# The user selected a character name | |
return {'username': data.get('character_name')} | |
class BattleNetOAuth2(BaseOAuth2): | |
""" battle.net Oauth2 backend""" | |
name = 'battlenet-oauth2' | |
REDIRECT_STATE = False | |
AUTHORIZATION_URL = 'https://eu.battle.net/oauth/authorize' | |
ACCESS_TOKEN_URL = 'https://eu.battle.net/oauth/token' | |
ACCESS_TOKEN_METHOD = 'POST' | |
# REVOKE_TOKEN_URL = 'https://accounts.google.com/o/oauth2/revoke' | |
REVOKE_TOKEN_METHOD = 'GET' | |
DEFAULT_SCOPE = ['wow.profile', ] | |
ID_KEY = 'accountId' | |
EXTRA_DATA = [ | |
('refresh_token', 'refresh_token', True), | |
('expires_in', 'expires'), | |
('token_type', 'token_type', True) | |
] | |
def get_characters(self, access_token): | |
""" | |
fetches the character list from the battle.net API. | |
:param access_token: the access token for the user which character list is fetched | |
:return: list of characters or empty list if the request fails. | |
""" | |
params = {'access_token': access_token} | |
if self.setting('API_LOCALE'): | |
params['locale'] = self.setting('API_LOCALE') | |
response = self.get_json('https://eu.api.battle.net/wow/user/characters', params=params) | |
return response.get('characters') or [] | |
def get_user_details(self, response): | |
""" Return user details from Battle.net account """ | |
return { | |
'battletag': response.get('battletag') | |
} | |
def user_data(self, access_token, *args, **kwargs): | |
""" Loads user data from service """ | |
user_data = self.get_json( | |
'https://eu.api.battle.net/account/user/battletag', | |
params={'access_token': access_token} | |
) | |
print("user_data:", user_data) | |
return user_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment