Beispiel #1
0
def ready(request, action=''):
    if 'oauth_verifier' not in request.REQUEST:
        return HttpResponseRedirect('/error')

    client = TwitterOAuthClient(request)
    token = OAuthRequestToken.all().filter('token', request.REQUEST['oauth_token']).get().OAuthToken()
    verifier = request.REQUEST['oauth_verifier']

    # get access token
    oauth_request = OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url)
    oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, token)
    (token, response) = client.fetch_access_token(oauth_request)

    # get user credentials
    params = cgi.parse_qs(response.content, keep_blank_values=False)
    userid = int(params['user_id'][0])

    user = User.all().filter('userid', userid).get()
    if user is None:
        user = User(username=params['screen_name'][0], userid=userid, token=token.key, secret=token.secret)
    else:
        user.token = token.key
        user.secret = token.secret
    user.put()

    request.session['token'] = token.key

    return HttpResponseRedirect('/%s' % action)
Beispiel #2
0
def login(request, action=''):
    user = User.all().filter('token', request.session.get('token', None)).get()
    if user is not None:
        return HttpResponseRedirect('/')
    client = TwitterOAuthClient(request)

    # get request token
    oauth_request = OAuthRequest.from_consumer_and_token(consumer, callback='%s%s' % (CALLBACK_URL, action), http_url=client.request_token_url)
    oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, None)
    token = client.fetch_request_token(oauth_request)

    # save request token
    OAuthRequestToken.save(token)

    # authorize request token
    oauth_request = OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url)
    return client.authorize_token(oauth_request)