Exemplo n.º 1
0
def refresh_access_token(token, user):
    token = get_refreshed_token(token)
    if not user.is_authenticated():
        return token
    try:
        ts = TokenStorageModel.objects.get(id=user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=user)
    ts.token = gdata.gauth.token_to_blob(token)
    ts.save()
    return token
Exemplo n.º 2
0
def google_auth_return(request):
    if 'code' not in request.REQUEST:
        return HttpResponse(u"Invalid Request")
    try:
        ts = TokenStorageModel.objects.get(id=request.user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=request.user)
    access_token = token.get_access_token(request.REQUEST)
    ts.token = gdata.gauth.token_to_blob(token=access_token)
    ts.save()
    return HttpResponseRedirect(reverse(home))
Exemplo n.º 3
0
def google_auth_return(request):
    if 'code' not in request.REQUEST:
        return HttpResponse(u"Invalid Request")
    if request.user.is_authenticated():
        try:
            ts = TokenStorageModel.objects.get(id=request.user)
        except TokenStorageModel.DoesNotExist:
            ts = TokenStorageModel(id=request.user)
        access_token = oauth2_token.get_access_token(request.REQUEST)
        ts.token = gdata.gauth.token_to_blob(token=access_token)
        ts.save()
    else:
        access_token = oauth2_token.get_access_token(request.REQUEST)
        request.session["access_token"] =\
            gdata.gauth.token_to_blob(token=access_token)
    if request.session.get('google_redirect_url'):
        return HttpResponseRedirect(request.session.get('google_redirect_url'))
    return HttpResponseRedirect(reverse(home))
Exemplo n.º 4
0
def refresh_access_token(token, user):
    try:
        ts = TokenStorageModel.objects.get(id=user)
    except TokenStorageModel.DoesNotExist:
        ts = TokenStorageModel(id=user)
    data = urllib.urlencode({
        'client_id': settings.GOOGLE_CLIENT_ID,
        'client_secret': settings.GOOGLE_CLIENT_SECRET,
        'refresh_token': token.refresh_token,
        'grant_type': 'refresh_token'
    })
    request = urllib2.Request(url='https://accounts.google.com/o/oauth2/token',
                              data=data)
    request_open = urllib2.urlopen(request)
    response = request_open.read()
    request_open.close()
    tokens = json.loads(response)
    token.access_token = tokens['access_token']
    ts.token = gdata.gauth.token_to_blob(token)
    ts.save()
    return token