Esempio n. 1
0
    def get(self):
        invitationCode = self.request.get('invitation_code')
        if not self.isValidInvitationCode(invitationCode):
            error_output(self, "<html><body>邀请码无效</body></html>", "text/html", 400)
            return
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        
        verifier = self.request.get("oauth_verifier").strip()
        twitterId = self.request.get("twitter_id").strip()
        if verifier == "" or twitterId == "":
            authUrl = auth.get_authorization_url()
            success_output(self, page_goto_sina_oauth % \
                {'url':authUrl, 
                 'invitation':invitationCode.encode('UTF-8'),
                 'token':auth.request_token.key, 
                 'secret':auth.request_token.secret})
        else:
            request_token = self.request.get("request_token")
            request_secret = self.request.get("request_secret")
            auth.set_request_token(request_token, request_secret)
            accessToken = auth.get_access_token(verifier)
            binding = SyncBinding.getOrInsertByInvitationCode(invitationCode)
            binding.lastTweetId = None
            binding.twitterId = twitterId
            binding.sinaAccessToken = accessToken.key
            binding.sinaAccessSecret = accessToken.secret
            binding.put()
            success_output(self, '''
<html><body>
<p>Twitter与新浪微博同步绑定成功</p>
<p>如需要修改绑定,请重新访问邀请链接</p>
</body></html>
            ''')
Esempio n. 2
0
 def init_2nd_step(self, verifier_num):
     info = user.get_app('sina', self.email)
     auth = OAuthHandler(APP_KEY, APP_SECRET)
     auth.set_request_token(info.get('request_token'), info.get('request_secret'))
     access = auth.get_access_token(verifier_num)
     user.update_app('sina', self.email, access_token=access.key, access_secret=access.secret)
     return True
Esempio n. 3
0
    def get(self):
        invitationCode = self.request.get('invitation_code')
        if not self.isValidInvitationCode(invitationCode):
            error_output(self, "<html><body>邀请码无效</body></html>", "text/html",
                         400)
            return
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)

        verifier = self.request.get("oauth_verifier").strip()
        twitterId = self.request.get("twitter_id").strip()
        if verifier == "" or twitterId == "":
            authUrl = auth.get_authorization_url()
            success_output(self, page_goto_sina_oauth % \
                {'url':authUrl,
                 'invitation':invitationCode.encode('UTF-8'),
                 'token':auth.request_token.key,
                 'secret':auth.request_token.secret})
        else:
            request_token = self.request.get("request_token")
            request_secret = self.request.get("request_secret")
            auth.set_request_token(request_token, request_secret)
            accessToken = auth.get_access_token(verifier)
            binding = SyncBinding.getOrInsertByInvitationCode(invitationCode)
            binding.lastTweetId = None
            binding.twitterId = twitterId
            binding.sinaAccessToken = accessToken.key
            binding.sinaAccessSecret = accessToken.secret
            binding.put()
            success_output(
                self, '''
<html><body>
<p>Twitter与新浪微博同步绑定成功</p>
<p>如需要修改绑定,请重新访问邀请链接</p>
</body></html>
            ''')
Esempio n. 4
0
 def GET(self):
     oauth_token = web.input().oauth_token
     oauth_verifier = web.input().oauth_verifier
     auth = OAuthHandler(oauth.APP_KEY, oauth.APP_SECRET)
     auth.set_request_token(session.rtKey[web.ctx.ip], session.rtSec[web.ctx.ip])
     access_token = auth.get_access_token(oauth_verifier)
     session.atKey[web.ctx.ip] = access_token.key
     session.atSec[web.ctx.ip] = access_token.secret
     raise web.seeother('/sinaweibo/timeline')
Esempio n. 5
0
def login_callback():
    # This is called by the provider when user has granted permission to your app
    verifier = request.args.get('oauth_verifier', None)
    auth = OAuthHandler(APP_KEY, APP_SECRET)
    request_token = session['oauth_request_token']
    del session['oauth_request_token']
    
    # Show the provider it's us really
    auth.set_request_token(request_token.key, request_token.secret)
    # Ask for a temporary access token
    session['oauth_access_token'] = auth.get_access_token(verifier)
    return render_template("login_callback.html")
Esempio n. 6
0
def login_callback():
    # This is called by the provider when user has granted permission to your app
    verifier = request.args.get('oauth_verifier', None)
    auth = OAuthHandler(APP_KEY, APP_SECRET)
    request_token = session['oauth_request_token']
    del session['oauth_request_token']

    # Show the provider it's us really
    auth.set_request_token(request_token.key, request_token.secret)
    # Ask for a temporary access token
    session['oauth_access_token'] = auth.get_access_token(verifier)
    return render_template("login_callback.html")
Esempio n. 7
0
def do_auth():
    auth = OAuthHandler(APP_KEY, APP_SECRET, BACK_URL)
    auth_url = auth.get_authorization_url()
    request_token_key = auth.request_token.key
    request_token_secret = auth.request_token.secret
    auth.set_request_token(request_token_key, request_token_secret)
    webbrowser.open(auth_url)
    verifier = input("Verifier: ").strip()
    access_token = auth.get_access_token(verifier)
    ATK = access_token.key
    ATS = access_token.secret
    auth.setAccessToken(ATK, ATS)
    api = API(auth)
    user = api.verify_credentials()
    logging("[AUTH]: We are uing API from account: [uid = %s, name = %s]" % (user.id, user.screen_name))
    return api
Esempio n. 8
0
def do_auth():
    auth = OAuthHandler(APP_KEY, APP_SECRET, BACK_URL)
    auth_url = auth.get_authorization_url()
    request_token_key = auth.request_token.key
    request_token_secret = auth.request_token.secret
    auth.set_request_token(request_token_key, request_token_secret)
    webbrowser.open(auth_url)
    verifier = input("Verifier: ").strip()
    access_token = auth.get_access_token(verifier)
    ATK = access_token.key
    ATS = access_token.secret
    auth.setAccessToken(ATK, ATS)
    api = API(auth)
    user = api.verify_credentials()
    logging("[AUTH]: We are uing API from account: [uid = %s, name = %s]" %
            (user.id, user.screen_name))
    return api
Esempio n. 9
0
 def get(self):
     print '123123123'
     verifier = self.get_argument('oauth_verifier', None)
     auth = OAuthHandler(SINA_CONSUME_KEY, SINA_CONSUME_SECRET)
     if not verifier:
         #self.SESSION['goto'] = self.get_argument('to_url', None)
         auth_url = auth.get_authorization_url()+'&oauth_callback='+self.request.protocol+'://'+self.request.host+'/people/auth/'
         self.SESSION['oauth_request_token'] = auth.request_token
         self.redirect(auth_url)
     else:
         request_token = self.SESSION['oauth_request_token']
         del self.SESSION['oauth_request_token']
         auth.set_request_token(request_token.key, request_token.secret)
         access_token = auth.get_access_token(verifier)
         self.SESSION['oauth_access_token'] = access_token
         me = WeiboAPI(auth).me()
         self.after_auth(me)
Esempio n. 10
0
def login(request):
    '''get a permanent access token'''
    session = get_current_session()
    if not request.GET.get('oauth_token'):
        '''login and save oauth token'''
        cur_url = _get_absolute_path(request)
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, callback = cur_url)
        auth_url = auth.get_authorization_url()
        # 需要保存request_token的信息,留做取access_token用
        session["oauth_token"] = auth.request_token.key
        session["oauth_token_secret"] = auth.request_token.secret
        return http.HttpResponseRedirect(auth_url)
    else:
        ''' Get the access token '''
        oauth_verifier = request.GET.get('oauth_verifier')
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        # 从session中取出request_token的信息
        auth.set_request_token(session["oauth_token"], session["oauth_token_secret"])
        auth.get_access_token(oauth_verifier)
        # save access token
        session['access_token_key'] = auth.access_token.key
        session['access_token_secret'] = auth.access_token.secret
        response = shortcuts.redirect('sinaweibo.views.mainPage')
        return response
        
        
    if 0:
        path = os.path.join(os.path.dirname(__file__), "templates/index.html")
        myself = api.get_user(id=1894001933)
        screen_name = myself. __getattribute__("screen_name")
        self.response.out.write(template.render(path,
                {"name": screen_name}))#dir(api.verify_credentials)}))#api.verify_credentials.name}))#screen_name}))

    if 0:
        params = {}
        params['test'] = 'nothing'
        return shortcuts.render_to_response('test.html', params)

    if 0:
        # add GET para and redirect
        response = shortcuts.redirect('sinaweibo.views.mainPage')
        response['Location'] += '?oauth_verifier=%s' % oauth_verifier
        return response
            
Esempio n. 11
0
    def post(self):
        t_name = self.request.get('t_name')
        s_name = self.request.get('s_name')
        s_request_key = self.request.get('s_request_key')
        s_request_secret = self.request.get('s_request_secret')
        t_request_key = self.request.get('t_request_key')
        t_request_secret = self.request.get('t_request_secret')
        t_pin = self.request.get('t_pin')
        s_pin = self.request.get('s_pin')
        self.response.out.write("""<html><head><title>Tui2Lang-Result</title></head><body><center>""")
        if t_name == "" or s_name =="" or s_pin == "":
            self.response.out.write("""<h2>4 Input can not be empty! <a href="/">Back</a></h2>""")
        else:
            sina = OAuthHandler(app_key,app_secret)
            sina.set_request_token(s_request_key,s_request_secret)
            s_access_token = sina.get_access_token(s_pin.strip())
            sina_api = API(sina)

            twitter = tweepy.OAuthHandler(consumer_key,consumer_secret)
            twitter.set_request_token(t_request_key,t_request_secret)
            t_access_token = twitter.get_access_token(t_pin.strip())
            twitter_api = tweepy.API(twitter);
            
            t_tl = twitter_api.user_timeline()
            t_last_id = t_tl[0].id_str
            t_last_text = replace_tweet(t_tl[0].text)
            

            oauth_user = OauthUser(key_name=s_name)
            oauth_user.twitter_name = t_name
            oauth_user.sina_name = s_name
            oauth_user.sina_access_key = s_access_token.key
            oauth_user.sina_access_secret = s_access_token.secret
            oauth_user.twitter_access_key = t_access_token.key
            oauth_user.twitter_access_secret = t_access_token.secret
            oauth_user.twitter_last_id = t_last_id
            oauth_user.put()

            try:
                sina_api.update_status(t_last_text)            
            except WeibopError,e:
                self.response.out.write(e)
            else:
Esempio n. 12
0
#获取授权页面网址.
auth_url = auth.get_authorization_url();
webbrowser.open(auth_url)
verifier = raw_input('PIN: ').strip() 
#取出请求令牌密钥(桌面应用跳过此处)
rtKey = auth.request_token.key;
rtSecret = auth.request_token.secret;
#   进行到这一步针对桌面应用和网页应用有两个不同的分支:
#   1、桌面应用将授权页面网址提供给用户,用户访问授权页面,输入用户名和密码并通过验证之后,获取到一个授权码,回到桌面应用中提交该授权码。
#   2、网页应用直接将用户引导至授权页面,引导前应将rtKey和rtSecret缓存到Session中。当用户在授权页面输入用户名和密码并通过验证之后,
#     授权页面会调用网页应用的回调页面,同时传递参数oauth_token和oauth_verifier,其中oauth_token应和rtKey相同(回调页面中需确认此处),
#     而oauth_verifier即为授权码,下文中简称为verifier。
#   有了授权码verifier之后,加上之前缓存在Session中的rtKey和rtSecret便可获取用户令牌密钥。

#设定请求令牌密钥(桌面应用跳过此句)
auth.set_request_token( rtKey, rtSecret );

#获取用户令牌密钥.
access_token = auth.get_access_token( verifier );
atKey = access_token.key;
atSecret = access_token.secret;
#   终于,我们获取到了用户令牌密钥atKey和atSecret,接下来的所有步骤都需要用这两个参数来验证用户的身份。

# 三、获取用户信息
from weibopy.error import WeibopError;

#设定用户令牌密钥.
auth.setToken( atKey, atSecret );
#绑定用户验证信息.
api = API(auth);