本文整理汇总了Python中twython.Twython.get_authorized_tokens方法的典型用法代码示例。如果您正苦于以下问题:Python Twython.get_authorized_tokens方法的具体用法?Python Twython.get_authorized_tokens怎么用?Python Twython.get_authorized_tokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twython.Twython
的用法示例。
在下文中一共展示了Twython.get_authorized_tokens方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: twitterCallback
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def twitterCallback(request):
oauthToken = request.session['requestToken']['oauth_token']
oauthTokenSecret = request.session['requestToken']['oauth_token_secret']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET, oauthToken, oauthTokenSecret)
authorizedTokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
request.session['userName'] = authorizedTokens['screen_name']
return redirect("/login/")
示例2: thanks
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
# tokens obtained from Twitter
oauth_token = request.session['request_token']['oauth_token']
oauth_token_secret = request.session['request_token']['oauth_token_secret']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
oauth_token, oauth_token_secret)
# retrieving tokens
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
# if tokens already exist, use them to login and redirect to the battle
try:
user = User.objects.get(username=authorized_tokens['screen_name'])
user.set_password(authorized_tokens['oauth_token_secret'])
user.save()
except User.DoesNotExist:
# mock a creation here; no email, password is just the token, etc. since no need for users in this app
user = User.objects.create_user(authorized_tokens['screen_name'], "[email protected]", authorized_tokens['oauth_token_secret'])
profile = TwitterProfile()
profile.user = user
profile.oauth_token = authorized_tokens['oauth_token']
profile.oauth_secret = authorized_tokens['oauth_token_secret']
profile.save()
user = authenticate(
username=authorized_tokens['screen_name'],
password=authorized_tokens['oauth_token_secret']
)
login(request, user)
return HttpResponseRedirect(redirect_url)
示例3: twitter_callback
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def twitter_callback(request):
response = HttpResponse()
context = {}
context['not_logged_in'] = False
oauth_verifier = request.GET['oauth_verifier']
twitter = Twython(
os.environ.get('consumer_key'),
os.environ.get('consumer_secret'),
request.session['request_token']['oauth_token'],
request.session['request_token']['oauth_token_secret'],
)
authorized_tokens = twitter.get_authorized_tokens(oauth_verifier)
request.session['oauth_token'] = authorized_tokens['oauth_token']
request.session['oauth_token_secret'] = authorized_tokens['oauth_token_secret']
twitter = Twython(
os.environ.get('consumer_key'),
os.environ.get('consumer_secret'),
request.session['oauth_token'],
request.session['oauth_token_secret'],
)
request.session['screen_name'] = twitter.verify_credentials()['screen_name']
context['user'] = request.session['screen_name']
rendered = render_to_string('webapp/search.html', context)
response.write(rendered)
return response
示例4: handle_twitter_callback
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def handle_twitter_callback():
twitter = Twython(app.config['TWITTER_API_KEY'],app.config['TWITTER_API_SECRET'],
session['oauth_token'], session['oauth_token_secret'])
success = True
try:
final_step = twitter.get_authorized_tokens(request.json['oauth_verifier'])
user_oauth_token = final_step['oauth_token']
user_oauth_token_secret = final_step['oauth_token_secret']
user_twitter = Twython(app.config['TWITTER_API_KEY'],app.config['TWITTER_API_SECRET'],
user_oauth_token, user_oauth_token_secret)
twitter_user_show = user_twitter.show_user(user_id=final_step['user_id'])
current_user.set_twitter_data(final_step['user_id'], final_step['screen_name'], twitter_user_show)
new_twitter_auth = TwitterAuth(current_user.get_id(), user_oauth_token, user_oauth_token_secret)
db.session.add(new_twitter_auth)
db.session.commit()
tasks.get_tweets_per_user.delay(current_user.get_id())
# db.session.close()
except:
# print 'error in twitter auth'
success = False
return jsonify({'success': success})
示例5: personalAuthentication
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def personalAuthentication():
allKeys = fileToList(TOKEN_FILE)
if len(allKeys[0]) > 4: # we've already saved the personal token
for key in authentication_keys.keys():
# The 4th item of the list of key data will be the key for a
# personal account
authentication_keys[key] = readToken(allKeys, key, 4)
else:
twitter = Twython(authentication_keys['APP_KEY'],
authentication_keys['APP_SECRET'])
auth = twitter.get_authentication_tokens()
import webbrowser
# Open a webpage with your twitter code
webbrowser.open(auth['auth_url'])
try:
auth_pin = input("Please enter the code from twitter: ")
except: # python 2.7
auth_pin = raw_input("Please enter the code from twitter: ")
# These are temporary, part of the overall authentication process
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
twitter = Twython(authentication_keys['APP_KEY'],
authentication_keys['APP_SECRET'], OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(auth_pin)
authentication_keys['OAUTH_KEY'] = final_step['oauth_token']
authentication_keys['OAUTH_SECRET'] = final_step['oauth_token_secret']
writeKeys(authentication_keys, TOKEN_FILE)
示例6: _authorized
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def _authorized():
"""
Callback for Twitter authentication.
"""
# Login failed
if request.args.get("denied", None) or request.args.get("oauth_token", "") != session["oauth_token"]:
session.pop("oauth_token", None)
session.pop("oauth_token_secret", None)
session.pop("screen_name", None)
return redirect(url_for("index"))
twitter = Twython(
secrets["TWITTER_CONSUMER_KEY"],
secrets["TWITTER_CONSUMER_SECRET"],
session["oauth_token"],
session["oauth_token_secret"],
)
auth = twitter.get_authorized_tokens(request.args.get("oauth_verifier"))
session["oauth_token"] = auth["oauth_token"]
session["oauth_token_secret"] = auth["oauth_token_secret"]
session["screen_name"] = auth["screen_name"]
return redirect(url_for("index"))
示例7: thanks
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
oauth_token = request.session['request_token']['oauth_token']
oauth_token_secret = request.session['request_token']['oauth_token_secret']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET, oauth_token, oauth_token_secret)
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
try:
user = User.objects.get(username=authorized_tokens['screen_name'])
except User.DoesNotExist:
user = User.objects.create_user(authorized_tokens['screen_name'], '[email protected]',
authorized_tokens['oauth_token_secret'])
profile = TwitterProfile()
profile.user = user
profile.oauth_token = authorized_tokens['oauth_token']
profile.oauth_secret = authorized_tokens['oauth_token_secret']
profile.save()
user = authenticate(
username=authorized_tokens['screen_name'],
password=authorized_tokens['oauth_token_secret']
)
login(request, user)
return HttpResponseRedirect(redirect_url)
示例8: callback
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def callback(Rate_id):
""" twitter로부터 callback url이 요청되었을때
최종인증을 한 후 트위터로 해당 사진과 커멘트를 전송한다.
"""
Log.info("callback oauth_token:" + request.args['oauth_token']);
Log.info("callback oauth_verifier:" + request.args['oauth_verifier']);
# oauth에서 twiter로 부터 넘겨받은 인증토큰을 세션으로 부터 가져온다.
OAUTH_TOKEN = session['OAUTH_TOKEN']
OAUTH_TOKEN_SECRET = session['OAUTH_TOKEN_SECRET']
oauth_verifier = request.args['oauth_verifier']
try:
# 임시로 받은 인증토큰을 이용하여 twitter 객체를 만들고 인증토큰을 검증한다.
twitter = Twython(current_app.config['TWIT_APP_KEY'],
current_app.config['TWIT_APP_SECRET'],
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)
# oauth_verifier를 통해 얻은 최종 인증토큰을 이용하여 twitter 객체를 새로 생성한다.
twitter = Twython(current_app.config['TWIT_APP_KEY'],
current_app.config['TWIT_APP_SECRET'],
final_step['oauth_token'],
final_step['oauth_token_secret'])
session['TWITTER'] = twitter
# 파라미터로 받은 Rate_id를 이용하여 해당 사진과 커멘트를 트위터로 전송한다.
__send_twit(twitter, Rate_id)
except TwythonError as e:
Log.error("callback(): TwythonError , "+ str(e))
session['TWITTER_RESULT'] = str(e)
return redirect(url_for('.show_all'))
示例9: Authentication
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def Authentication():
APP_KEY = 'b1WTyzKmLnG7KwnDcYpiQ'
APP_SECRET = 'gzKci8Gys0i831zt7gPq3fEpG4qq9kgOINfbKhS8'
twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()
OAUTH_TOKEN = auth ['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
webbrowser.open(auth['auth_url'])
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(str(raw_input("Please enter the PIN: "))) #atm pin must be entered through the terminal
OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']
print APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
return twitter
示例10: thanks
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
"""A user gets redirected here after hitting Twitter and authorizing your app to use their data.
This is the view that stores the tokens you want
for querying data. Pay attention to this.
"""
# Now that we've got the magic tokens back from Twitter, we need to exchange
# for permanent ones and store them...
oauth_token = request.session['request_token']['oauth_token']
oauth_token_secret = request.session['request_token']['oauth_token_secret']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
oauth_token, oauth_token_secret)
# Retrieve the tokens we want...
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
# If they already exist, grab them, login and redirect to a page displaying stuff.
try:
user = User.objects.get(username=authorized_tokens['screen_name'])
except User.DoesNotExist:
# We mock a creation here; no email, password is just the token, etc.
user = User.objects.create_user(authorized_tokens['screen_name'], "[email protected]", authorized_tokens['oauth_token_secret'])
profile = TwitterProfile()
profile.user = user
profile.oauth_token = authorized_tokens['oauth_token']
profile.oauth_secret = authorized_tokens['oauth_token_secret']
profile.save()
user = authenticate(
username=authorized_tokens['screen_name'],
password=authorized_tokens['oauth_token_secret']
)
login(request, user)
return HttpResponseRedirect(redirect_url)
示例11: authorized
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def authorized(request, redirect_url=settings.AUTHORIZE_COMPLETE_URL):
"""
This function is a callback from Twitter which is triggered after
the user signs in and allows authorization of their Twitter account.
"""
try:
twitter = Twython(
settings.TWITTER_KEY,
settings.TWITTER_SECRET,
request.session['request_token']['oauth_token'],
request.session['request_token']['oauth_token_secret'],
)
# Get the access token to complete the three legged oauth handshake
twitter_info = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
# Something unusual happened from the redirect back from twitter and
# nothing was stored in the session, redirect back to homepage.
except KeyError:
return HttpResponseRedirect(redirect_url)
# only store twitter info in the session if its valid otherwise the user
# hit cancel and didn't actually sign in
if 'oauth_token_secret' in twitter_info and 'user_id' in twitter_info:
request.session['twitter_info'] = twitter_info
return HttpResponseRedirect(redirect_url)
示例12: getAuthTokens
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def getAuthTokens():
f_authTokens = open(config.FILE_TMP_TOKENS, 'w')
# get first auth
twitter = Twython(config.APP_KEY, config.APP_SECRET)
firstAuth = twitter.get_authentication_tokens()
# get auth pin
print 'auth url: ' + firstAuth['auth_url']
authPin = getpass('pin: ')
# get second auth
twitter = Twython(
config.APP_KEY,
config.APP_SECRET,
firstAuth['oauth_token'],
firstAuth['oauth_token_secret'])
secondAuth = twitter.get_authorized_tokens(authPin)
# save tokens to a file
for key in secondAuth:
line = key + '\t' + secondAuth[key] + '\n'
f_authTokens.write(line)
f_authTokens.close()
return secondAuth
示例13: GET
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def GET(self):
data = web.input()
OAUTH_TOKEN = data.oauth_token
OAUTH_TOKEN_SECRET = web.cookies().get("OAUTH_TOKEN_SECRET")
APP_KEY = "##################"
APP_SECRET = "####################"
print OAUTH_TOKEN
print OAUTH_TOKEN_SECRET
heeey = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = heeey.get_authorized_tokens(data.oauth_verifier)
OAUTH_TOKEN = final_step["oauth_token"]
print final_step
print "Ponto 1"
print OAUTH_TOKEN
print final_step["screen_name"]
OAUTH_TOKEN_SECRET = str(final_step).split("'")[3]
print OAUTH_TOKEN_SECRET
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
print "Ponto 1.5"
web.setcookie("OAUTH_TOKEN", OAUTH_TOKEN, domain=".phckopper.org")
web.setcookie("OAUTH_TOKEN_SECRET", OAUTH_TOKEN_SECRET, domain=".phckopper.org")
web.setcookie("username", final_step["screen_name"], domain=".phckopper.org")
db.insert(
"users",
username=final_step["screen_name"],
access_token=OAUTH_TOKEN,
access_token_secret=OAUTH_TOKEN_SECRET,
bio="Heeeey new user here!",
profile_picture="/robogram/gear.png",
)
raise web.seeother("/")
示例14: add_twitter_token
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def add_twitter_token(request, **kwargs):
if request.method == 'GET':
twitter = Twython(
twitter_token = TWITTER_CONSUMER_KEY,
twitter_secret = TWITTER_CONSUMER_SECRET,
oauth_token = request.session['request_token']['oauth_token'],
oauth_token_secret = request.session['request_token']['oauth_token_secret'],
)
oauth_verifier = request.GET['oauth_verifier']
try:
authorized_tokens = twitter.get_authorized_tokens(oauth_verifier)
if 'oauth_token' not in authorized_tokens:
raise TwythonError(authorized_tokens)
oauth_token = authorized_tokens['oauth_token']
oauth_secret = authorized_tokens['oauth_token_secret']
tw = addTwitterToken(token=oauth_token, secret=oauth_secret)
except TwythonError as e:
kwargs['error'] = e
return render_to_response('news/twitter_error.html', kwargs,
context_instance=RequestContext(request))
return render_to_response('news/twitter.html', {},
context_instance=RequestContext(request))
else:
raise Http404
示例15: save_twitter_user
# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_authorized_tokens [as 别名]
def save_twitter_user():
"""
Save the user received from twitter auth or log them in if already registered
"""
t = Twython(
app_key=CONFIGURATION.TW_OAUTH_KEY,
app_secret=CONFIGURATION.TW_OAUTH_SECRET,
oauth_token=session["request_token"]["oauth_token"],
oauth_token_secret=session["request_token"]["oauth_token_secret"],
)
authorized_tokens = t.get_authorized_tokens()
try:
user = User.collection.find_one({"username": authorized_tokens["screen_name"]})
if user:
session["user"] = user
else:
# create a new user via twitter
# oauth = dict(authorized_tokens.items() + dict(provider='twitter').items())
oauth = {"twitter": dict(authorized_tokens.items() + dict(auth=True).items())}
session["user"] = User.create(username=authorized_tokens["screen_name"], oauth=oauth)
print session["user"]
return redirect(url_for(".root"))
except DuplicateKeyError:
# user is trying to reinsert
return "fail"