def twitter_oauth1(user_id, token_nickname, country_filter):
consumer = oauth.Consumer(auth.consumer_key, auth.consumer_secret)
client = oauth.Client(consumer)
# Step 1: Get a request token. This is a temporary token that is used for
# having the user authorize an access token and to sign the request to obtain
# said access token.
resp, content = client.request(request_token_url, "GET")
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(urlparse.parse_qsl(content))
auth_tokens = db.OAuthToken.query(
db.OAuthToken.user_id == user_id, db.OAuthToken.token_nickname == token_nickname, db.OAuthToken.application == db.APP_TWITTER
).fetch(1)
if auth_tokens:
auth_token = auth_tokens[0]
else:
auth_token = db.OAuthToken()
auth_token.user_id = user_id
auth_token.token_nickname = token_nickname
auth_token.application = db.APP_TWITTER
auth_token.temp_oauth_token = request_token['oauth_token']
auth_token.temp_oauth_token_secret = request_token['oauth_token_secret']
if country_filter:
auth_token.country_filters += country_filter.upper()
auth_token.put()
# Step 2: Redirect to the provider. Since this is a CLI script we do not
# redirect. In a web application you would redirect the user to the URL
# below.
return "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
# user comes to:
# /sign-in-with-twitter/?
# oauth_token=NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0&
# oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY
评论列表
文章目录