本文整理汇总了Python中tweepy.OAuthHandler.get_authorization_url方法的典型用法代码示例。如果您正苦于以下问题:Python OAuthHandler.get_authorization_url方法的具体用法?Python OAuthHandler.get_authorization_url怎么用?Python OAuthHandler.get_authorization_url使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tweepy.OAuthHandler
的用法示例。
在下文中一共展示了OAuthHandler.get_authorization_url方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SanchanOAuthHandler
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
class SanchanOAuthHandler():
def __init__(self, config):
self.config = config
self.keys = self.config.OAuthKeys()
self.oauth = OAuthHandler(self.keys['consumer_key'],
self.keys['consumer_secret'],
secure = True
)
self.oauth.set_access_token(self.keys['access_token_key'], self.keys['access_token_secret'])
def authenticate(self):
return self.oauth
def request(self):
print "Authorize this app via this URL: "
print self.oauth.get_authorization_url()
pincode = raw_input('Then, input the proposed PIN: ')
try:
self.oauth.get_access_token(verifier=pincode)
except error.TweepError, e:
print e
print "[EMERG] Authentication error!"
sys.exit(1)
print "Put these access keys into your config.yml:"
print "access_token: " + self.oauth.access_token.key
print "access_token_secret: " + self.oauth.access_token.secret
sys.exit(0)
示例2: TestTweepyAuthentication
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
class TestTweepyAuthentication(TestCase):
def setUp(self):
self.auth = OAuthHandler(apikeys.TWITTER_CONSUMER_KEY, apikeys.TWITTER_CONSUMER_SECRET)
self.auth.set_access_token(apikeys.TWITTER_ACCESS_TOKEN, apikeys.TWITTER_ACCESS_KEY)
self.invalid_auth = OAuthHandler("WRONG_CONSUMER_KEY", "WRONG_CONSUMER_SECRET")
self.invalid_auth.set_access_token("WRONG_ACCESS_TOKEN", "WRONG_ACCESS_KEY")
self.blank_auth = OAuthHandler("", "")
self.blank_auth.set_access_token("", "")
def test_tweepy_api_authentication_with_valid_fields(self):
"""
When valid set of authentication keys are passed, it gets back
Tests the tweepy api for valid set of API key.
"""
self.assertIsNotNone(self.auth.get_authorization_url())
def test_tweepy_api_authentication_with_invalid_fields(self):
"""
Tests the tweepy api for invalid set of API key.
"""
with self.assertRaises(tweepy.TweepError):
self.invalid_auth.get_authorization_url()
def test_tweepy_api_authentication_with_blank_fields(self):
"""
Tests the tweepy api for set of blank fields.
"""
with self.assertRaises(tweepy.TweepError):
self.blank_auth.get_authorization_url()
def test_tweepy_api__with_valid_phrase(self):
"""
Tests the tweepy api with valid phrase.
"""
api = tweepy.API(self.auth)
self.assertIsNotNone(api.search(q="word"))
def test_tweepy_api_with_empty_string(self):
"""
Tweepy should give TweepError when query is an empty string.
"""
api = tweepy.API(self.auth)
with self.assertRaises(tweepy.TweepError):
api.search(q="")
def test_tweepy_api_with_valid_phrase_but_invalid_authentication(self):
"""
Tweepy should give TweepError when given invalid set of API keys.
"""
api = tweepy.API(self.invalid_auth)
with self.assertRaises(tweepy.TweepError):
api.search(q="word")
示例3: run
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def run():
print 'Input your application info.'
consumer_key = raw_input('Consumer Key: ')
consumer_secret = raw_input('Consumer Secret: ')
auth = OAuthHandler(consumer_key,
consumer_secret)
print "Visit the following url and allow TweetHandler to access."
print '>>> %s' % auth.get_authorization_url()
print ''
vcode = raw_input('Enter verification code: ')
token = auth.get_access_token(vcode)
print 'OK. You can setup TweetHander with following code.'
print """ ----
from tweethandler import TweetHandler
import logging
th = TweetHandler('%s',
'%s',
'%s',
'%s')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
th.setLevel(logging.DEBUG)
logger.addHandler(th)
logger.info('Your log message')
""" % (consumer_key, consumer_secret,
token.key, token.secret)
示例4: twitter_login
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def twitter_login(request):
"""
ログインを行う。
:param request: リクエストオブジェクト
:type request: django.http.HttpRequest
:return: 遷移先を示すレスポンスオブジェクト
:rtype: django.http.HttpResponse
"""
# 認証URLを取得する
oauth_handler = OAuthHandler(
settings.CONSUMER_KEY,
settings.CONSUMER_SECRET,
request.build_absolute_uri(reverse(twitter_callback))
)
authorization_url = oauth_handler.get_authorization_url()
# リクエストトークンをセッションに保存する
request.session['request_token'] = oauth_handler.request_token
# ログイン完了後のリダイレクト先URLをセッションに保存する
if 'next' in request.GET:
request.session['next'] = request.GET['next']
# 認証URLにリダイレクトする
return HttpResponseRedirect(authorization_url)
示例5: __oauth_login
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def __oauth_login(self, next):
'''This method redirects the user to the authenticating form
on authentication server if the authentication code
and the authentication token are not available to the
application yet.
Once the authentication code has been received this method is
called to set the access token into the session by calling
accessToken()
'''
if not self.accessToken():
# setup the client
auth = OAuthHandler( self.client_id
, self.client_secret
, callback=self.__redirect_uri(next) )
redirect_url = None
try:
redirect_url = auth.get_authorization_url()
self.session.request_token = auth.request_token
except tweepy.TweepError:
print 'Error! Failed to get request token.'
if redirect_url:
HTTP = self.globals['HTTP']
raise HTTP(307,
"You are not authenticated: you are being redirected to the <a href='" + redirect_url + "'> authentication server</a>",
Location=redirect_url)
return None
示例6: _get_twitter_access
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def _get_twitter_access(self, username):
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
url = auth.get_authorization_url()
print 'Go here: %s and enter the corresponding PIN' % url
pin = raw_input('PIN:')
auth.get_access_token(pin)
return (auth.access_token.key, auth.access_token.secret)
示例7: authorize
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def authorize(consumer_token, consumer_secret):
auth = OAuthHandler(consumer_token, consumer_secret)
try:
redirect_url = auth.get_authorization_url()
print 'URL: %s' % redirect_url
except TweepError:
print 'Error! Failed to get request token.'
return auth
示例8: login
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def login():
auth = OAuthHandler(app.config['CONSUMER_KEY'], app.config['CONSUMER_SECRET'], url_for('authorized', _external=True))
try:
redirect_url = auth.get_authorization_url()
session['twitter_oauth']= [auth.request_token.key, auth.request_token.secret]
return redirect(redirect_url)
except TweepError:
print "Fail lol"
return 'Something went wrong, panic!'
示例9: Twit
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
class Twit(object):
def __init__(self,consumer_token, access_token=None):
if not isinstance(consumer_token, Token): raise Exception("consumer_token is invalid type!")
self.auth = OAuthHandler(consumer_token.key, consumer_token.secret)
self.api = None
if access_token != None and isinstance(access_token, Token):
# ok
self.auth.set_access_token(access_token.key, access_token.secret)
self.api = self._get_api(self.auth)
def _get_api(self, auth):
return API(auth,retry_count=3,cache=tweepy.MemoryCache(timeout=60))
def get_auth_url(self):
return self.auth.get_authorization_url()
def get_access_token(self, pin):
self.auth.get_access_token(pin)
self.api = self._get_api(self.auth)
return Token(self.auth.access_token.key, self.auth.access_token.secret)
@classmethod
def get_msg_length(cls, msg):
"""
https://dev.twitter.com/docs/api/1.1/get/help/configuration
"""
msg = re.sub(RegexHttpUrl, "0123456789012345678901", msg)
msg = re.sub(RegexHttpsUrl, "01234567890123456789012", msg)
return len(msg)
@classmethod
def validate_msg(cls, msg):
"""
Args:
msg: str, 投稿するメッセージ
Return:
(is_valid:bool, reminder:int)
"""
max_msg_length = 140
is_valid = False
msg_length = cls.get_msg_length(msg)
reminder = 140 - msg_length
if 0 < msg_length <= max_msg_length: is_valid = True
return (is_valid, reminder)
def post(self, msg):
return self.api.update_status(status=msg)
def verify_credentials(self, include_entities=True, skip_status=False):
return self.api.verify_credentials(include_entities=include_entities, skip_status=skip_status)
def get_user_timeline(self, screen_name, count=50):
return self.api.user_timeline(screen_name=screen_name, count=count)
def destroy_status(self, status_id):
return self.api.destroy_status(id=status_id)
示例10: __init__
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
class WTweetUser:
def __init__(self):
_CONSUMER_KEY = '9o4scrLHTFt7NzyVxD5Q'
_CONSUMER_SECRET = 'xlrPmai1QgNTRQTf86inp1bzEFLgEXD7XuN5ZENKAU'
self.__auth_handler = OAuthHandler(_CONSUMER_KEY, _CONSUMER_SECRET)
self.__access_token = None
def set_access_token(self, access_token):
self.__access_token = self.__auth_handler.set_access_token(key=access_token['key'], secret=access_token['secret'])
def get_auth_handler(self):
return self.__auth_handler
def logout(self):
with loadfile('w') as f:
f.truncate()
f.close()
print 'logout successful!'
def login(self):
try:
auth_url = self.__auth_handler.get_authorization_url()
print 'give me a pin code from url: {0}'.format(auth_url)
verifier = raw_input('enter a pin code: ').strip()
self.__auth_handler.get_access_token(verifier=verifier)
access_token = { 'key': self.__auth_handler.access_token.key, 'secret': self.__auth_handler.access_token.secret }
with loadfile('w+') as f:
f.truncate()
f.write(JSONEncoder().encode(access_token))
f.close()
except TweepError as err:
print err.__str__()
return WTweetTimeline(self)
def is_login(self):
with loadfile('r') as f:
cfg = f.readline()
f.close()
access_token = JSONDecoder().decode(cfg)
if 'key' in access_token and 'secret' in access_token:
self.set_access_token(access_token=access_token)
return True
else:
return False
def get_timeline(self):
with loadfile('r') as f:
cfg = f.readline()
f.close()
self.set_access_token(JSONDecoder().decode(cfg))
return WTweetTimeline(self)
示例11: twitauth
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def twitauth():
auth=OAuthHandler(consumer_token,consumer_secret,"http://127.0.0.1:8000/high_on_twitter/default/callback")
auth_url=auth.get_authorization_url()
print "auth_url",auth_url
aa={}
aa['key']=auth.request_token.key
aa['secret']=auth.request_token.secret
session.request_token=aa
print "request-token,key,secret twitauth",auth.request_token.key,auth.request_token.secret
#session.set('request_token', (auth.request_token.key,auth.request_token.secret))
redirect(auth_url)
'''print auth_url
示例12: auth
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def auth(request):
"""
Perform authentication with Twitter
"""
# start the OAuth process, set up a handler with our details
oauth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
# direct the user to the authentication url
auth_url = oauth.get_authorization_url()
response = HttpResponseRedirect(auth_url)
# store the request token
request.session['unauthed_token_twitterexp'] = (oauth.request_token.key, oauth.request_token.secret)
return response
示例13: twitter_oauth_request
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def twitter_oauth_request(request):
callback = reverse('twitter_oauth_verify')
callback = request.build_absolute_uri(callback)
handler = OAuthHandler(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, callback)
redirect_url = handler.get_authorization_url(signin_with_twitter=True)
request.session['request_token_key'] = handler.request_token.key
request.session['request_token_secret'] = handler.request_token.secret
return redirect(redirect_url)
示例14: testoauth
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def testoauth(self):
auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
# test getting access token
auth_url = auth.get_authorization_url()
print('Please authorize: ' + auth_url)
verifier = raw_input('PIN: ').strip()
self.assert_(len(verifier) > 0)
access_token = auth.get_access_token(verifier)
self.assert_(access_token is not None)
# build api object test using oauth
api = API(auth)
s = api.update_status('test %i' % random.randint(0, 1000))
api.destroy_status(s.id)
示例15: get_auth
# 需要导入模块: from tweepy import OAuthHandler [as 别名]
# 或者: from tweepy.OAuthHandler import get_authorization_url [as 别名]
def get_auth():
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
if not os.path.exists(CREDENTIALS_FILE):
authorization_url = auth.get_authorization_url()
webbrowser.open_new(authorization_url)
pin = input('PIN: ')
auth.get_access_token(pin)
cred = {
'ACCESS_TOKEN': auth.access_token,
'ACCESS_SECRET': auth.access_token_secret
}
fp = open(CREDENTIALS_FILE, 'wb')
pickle.dump(cred, fp)
fp.close()
else:
fp = open(CREDENTIALS_FILE, 'rb')
cred = pickle.load(fp)
fp.close()
auth.set_access_token(cred['ACCESS_TOKEN'], cred['ACCESS_SECRET'])
return auth