Exemplo n.º 1
0
def start_auth(request, fail_redirect='/account/other_services/'):
    consumer = OAuthConsumer(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)
    # Request the OAuth Token
    req = OAuthRequest().from_consumer_and_token(consumer, http_url=TWITTER_REQUEST_TOKEN_URL,
        parameters={}, http_method="POST")
    req.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, None)
    try:
        res = urllib.urlopen(TWITTER_REQUEST_TOKEN_URL, req.to_postdata())
        requestToken = OAuthToken.from_string(res.read())
        # Authorise the OAuth Token
        oauth_request = OAuthRequest().from_consumer_and_token(consumer, http_url=TWITTER_AUTHORIZE_URL, 
            parameters={}, http_method="GET", token=requestToken)
        oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, requestToken)
        return HttpResponseRedirect(oauth_request.to_url())
    except IOError:
        request.user.message_set.create(
            message=ugettext(u"Twitter authorization failed.")
        )
        return HttpResponseRedirect(fail_redirect)
Exemplo n.º 2
0
def get_credentials_from_request(request):
    token = OAuthToken(request.GET.get("oauth_token"), "")
    consumer = OAuthConsumer(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET)
    oauth_request = OAuthRequest().from_consumer_and_token(consumer, http_url=TWITTER_REQUEST_ACCESS_TOKEN_URL,
        parameters={}, http_method="GET", token=token)
    oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, token)
    try:
        res = urllib.urlopen(TWITTER_REQUEST_ACCESS_TOKEN_URL, oauth_request.to_postdata())
        accessToken = OAuthToken.from_string(res.read())
        # verify the access token
        verify_request = OAuthRequest().from_consumer_and_token(consumer, http_url=TWITTER_VERIFY_CREDENTIALS,
            http_method="GET", token=accessToken)
        verify_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, accessToken)
        res = urllib.urlopen(verify_request.to_url())
        json_response = simplejson.loads(res.read())
        if json_response['screen_name']:
            return accessToken
            request.user.message_set.create(
                message=ugettext(u"Twitter authorization failed.")
            )
    except IOError:
        request.user.message_set.create(
            message=ugettext(u"Twitter authorization failed.")
        )