Ejemplo n.º 1
0
    def get(self):
        oauth_token = self.request.get("oauth_token", None)
        oauth_verifier = self.request.get("oauth_verifier", None)
        if oauth_token is None:
            # Invalid request!
            self.response.out.write(template.render('templates/error.html', {
                    'message': 'Missing required parameters!'
            }))
            return

        # Lookup the request token
        request_token = OAuthToken.gql("WHERE token_key=:key", key=oauth_token).get()
        if request_token is None:
            # We do not seem to have this request token, show an error.
            self.response.out.write(template.render('templates/error.html', {'message': 'Invalid token!'}))
            return

        # Rebuild the auth handler
        auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_request_token(request_token.token_key, request_token.token_secret)

        # Fetch the access token
        try:
            auth.get_access_token(oauth_verifier)
            username = auth.get_username();
        except tweepy.TweepError, e:
            # Failed to get access token
            self.response.out.write( template.render('templates/error.html', {'message': e}))
            return
Ejemplo n.º 2
0
    def get(self):
        oauth_token = self.request.get("oauth_token", None)
        oauth_verifier = self.request.get("oauth_verifier", None)
        if oauth_token is None:
            # Invalid request!
            self.response.out.write(template.render('templates/error.html', {
                'message': 'Missing required parameters!'
            }))
            return

        # Lookup the request token
        request_token = OAuthToken.gql(
            "WHERE token_key=:key", key=oauth_token).get()
        if request_token is None:
            # We do not seem to have this request token, show an error.
            self.response.out.write(
                template.render('templates/error.html', {'message': 'Invalid token!'}))
            return

        # Rebuild the auth handler
        auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, secure=True)
        auth.set_request_token(
            request_token.token_key, request_token.token_secret)

        # Fetch the access token
        try:
            auth.get_access_token(oauth_verifier)
            username = auth.get_username()
        except tweepy.TweepError, e:
            # Failed to get access token
            self.response.out.write(
                template.render('templates/error.html', {'message': e}))
            return
Ejemplo n.º 3
0
class MainPage(webapp2.RequestHandler):

    def get(self):
        # Build a new oauth handler and display authorization url to user.
        auth = tweepy.OAuthHandler(
            CONSUMER_KEY,
            CONSUMER_SECRET,
            CALLBACK,
            secure=True
        )
        try:
            url = auth.get_authorization_url()
            self.response.out.write(
                template.render('templates/home.html',
                                {"auth_url": url}))
        except tweepy.TweepError, e:
            # Failed to get a request token
            self.response.out.write(
                template.render('templates/error.html', {'message': e}))
            return

        # We must store the request token for later use in the callback page.
        request_token = OAuthToken(
            token_key=auth.request_token.key,
            token_secret=auth.request_token.secret
        )
        request_token.put()
Ejemplo n.º 4
0
    def get(self):
        oauth_token = self.request.get("oauth_token", None)
        oauth_verifier = self.request.get("oauth_verifier", None)

        if oauth_token is None:
            # Invalid request!
            msg = {'message': 'Missing required parameters!'}
            path = os.path.join(os.path.dirname(__file__), 'view', 'error.html')
            self.response.out.write(template.render(path, msg))
            return

        # lookup the request token
        request_token = OAuthToken.gql("WHERE token_key=:key", key=oauth_token).get()
        if request_token is None:
            # We do not seem to have this request token, show an error.
            msg = {'message': 'Invalid token!'}
            path = os.path.join(os.path.dirname(__file__), 'view', 'error.html')
            self.response.out.write(template.render(path, msg))
            return

        auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_request_token(request_token.token_key, request_token.token_secret)
        request_token.delete()

        # fetch the access token
        try:
            auth.get_access_token(oauth_verifier)
        except tweepy.TweepError, e:
            # Failed to get access token
            msg = {'message': e}
            path = os.path.join(os.path.dirname(__file__), 'view', 'error.html')
            self.response.out.write(template.render(path, msg))
            return
Ejemplo n.º 5
0
def authorized(provider):
    if provider in oauth_providers:
        resp = oauth_providers[provider].authorized_response()
        if resp is None:
            flash('Authorization failed.')
        else:
            raw_token = resp.get(oauth_response_keys[provider])
            session['oauth_token'] = raw_token

            token = OAuthToken.query.filter_by(provider=provider, token=raw_token).first()
            if token is None:
                user = current_user if current_user.is_authenticated else User()
                token = OAuthToken(provider=provider, token=raw_token, user=user)
                db.session.add(user)
                db.session.add(token)
                db.session.commit()
            else:
                user = token.user

            login_user(user, True)

        return redirect(request.args.get('next') or url_for('index'))
    else:
        return abort(404)