Enabling Battle.net OAuth2 in python-social-auth

Having a running setup, using OAuth2 login with Django & python-social-auth==0.1.19, you can simply create a file bnet.py within the social/backends folder:

from social.backends.oauth import BaseOAuth2
class BnetOAuth2(BaseOAuth2):
    """Battlenet OAuth2 authentication backend"""
    name = 'bnet-oauth2'
    EMAIL_SUFFIX = "code-corner.de"
    AUTHORIZATION_URL = 'https://eu.battle.net/oauth/authorize'
    ACCESS_TOKEN_URL = 'https://eu.battle.net/oauth/token'
    DEFAULT_SCOPE = ['wow.profile',
    EXTRA_DATA = [
        ('refresh_token', 'refresh_token', True),
        ('expires_in', 'expires'),
        ('token_type', 'token_type', True)
    def asd_get_user_id(self, details, response):
        if self.setting('USE_UNIQUE_USER_ID', False):
            return response['email']
            return response['username']
    def get_user_details(self, response):
        return {'username': response.get('username', ''),
                'email': response.get('email', ''),
                'fullname': response.get('name', ''),
                'first_name': response.get('given_name', ''),
                'last_name': response.get('family_name', '')}
    def user_data(self, access_token, *args, **kwargs):
        id = self.get_json(
            params={'access_token': access_token}
        btag = self.get_json(
            params={'access_token': access_token}
        first_name, last_name = btag.split("#")
        return {"id": id,
                "username": first_name,
                "email": "%d@%s" % (id, self.EMAIL_SUFFIX),
                "family_name": last_name,
                "given_name": first_name,
                "name": btag}

Finally activate the created backend and don’t forget to add the BNet API keys to your django settings. Now go and try the login.

AUTHENTICATION_BACKENDS = ('social.backends.bnet.BnetOAuth2')

After login, a new user should be created using the part of the battle tag before the # as username and first_name and the number behind the # as last_name. The email will be filled using the BNet account id and a user-defined suffix.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>