def oauth_callback(request): api = FlattrAPI(secrets.FLATTR_API_KEY,secrets.FLATTR_API_SECRET) # Grab the request token and verifier verifier = request.GET["oauth_verifier"] key = request.GET["oauth_token"] secret = APIToken.objects.get(id=key).secret print "USING REQ TOKEN", key, secret token = api.make_token(key,secret) # Convert it into an access token token = api.claim_access_token(token,verifier) # Record the token with the current user, creating if necessary. try: user = User.objects.get(id=api.user.id) except User.DoesNotExist: user = User.objects.create(id=api.user.id, username=api.user.username, email=api.user.email) user.set_unusable_password() user.save() t = APIToken.objects.create(id=token.key, secret=token.secret, user=api.user.id) t.save() # Log them in as this user. login(request,user) # Redirect to wherever they wanted to go. next = request.GET.get("next","/") return HttpResponseRedirect(request.build_absolute_uri(next))
def check_login(request,*args,**kwds): if not request.user.is_authenticated(): api = FlattrAPI(secrets.FLATTR_API_KEY,secrets.FLATTR_API_SECRET) callback = reverse(oauth_callback) callback += "?next=" + urllib.quote(request.get_full_path(),"") callback = request.build_absolute_uri(callback) (token,url) = api.request_access_token(callback,"click") print "OBTAINING REQ TOKEN", token.key, token.secret t = APIToken.objects.create(id=token.key,secret=token.secret) t.save() print "REDIRECTING TO", url return HttpResponseRedirect(url) return view(request,*args,**kwds)