示例#1
0
def token(request):
    if 'error' in request.GET:
        raise Exception(request.GET['error'])
    elif 'code' not in request.GET:
        raise Exception('No authorization code given')
    r = requests.post(settings.GLITCH_API_URL+'/oauth2/token?', {
        'grant_type': 'authorization_code',
        'code': request.GET['code'],
        'client_id': settings.GLITCH_API_CREDENTIALS['key'],
        'client_secret': settings.GLITCH_API_CREDENTIALS['secret'],
        'redirect_uri': request.build_absolute_uri(reverse('glitchtools-users-token')),
    }, timeout=2)
    if r.status_code != 200:
        raise Exception(r.content)
    token = json.loads(r.content)
    auth = request.glitch_api.auth.check(oauth_token=token['access_token'])
    scope = GlitchUser.SCOPES_MAP[token['scope']]
    user = authenticate(username=auth['player_name'], tsid=auth['player_tsid'], token=token['access_token'], scope=scope)
    if user.glitch_user.scope < scope:
        update(user.glitch_user, token=token['access_token'], scope=scope)
    django_login(request, user)
    next = request.GET.get('state', '/')
    if not next.startswith('/'):
        next = '/'
    return HttpResponseRedirect(request.build_absolute_uri(next))
示例#2
0
 def __call__(self, **kwargs):
     if 'oauth_token' not in kwargs and self._token:
         kwargs['oauth_token'] = self._token
     timeout = kwargs.pop('timeout', None) or self._timeout
     resp_data = self._request(self._service_url + '/simple/' + self._service_name, kwargs, timeout)
     try:
         resp = json.loads(resp_data)
     except Exception, e:
         raise GlitchAPIError(str(e))