def login(): client = OAuthClient(key=DOUBAN_API_KEY,secret=DOUBAN_API_SECRET) request_token,request_token_secret = client.get_request_token() session['token'] = request_token session['secret'] = request_token_secret url = client.get_authorization_url(request_token, request_token_secret, callback='http://127.0.0.1:5000/callback') return redirect(url)
def callback(): client = OAuthClient(key=DOUBAN_API_KEY,secret=DOUBAN_API_SECRET) request_token = session['token'] request_token_secret = session['secret'] access_token,access_token_secret,ignore = client.get_access_token(key=request_token,secret=request_token_secret) session['access_token'] = access_token session['access_secret'] = access_token_secret return redirect('/mail')
def home(request): render_para = {} #if no session found, a new user. if not request.session.get('access_token_key', False): client = OAuthClient(key=API_KEY, secret=API_SECRET) key, secret = client.get_request_token() auth_url = client.get_authorization_url(key, secret, callback = HOST) render_para['auth_url'] = auth_url return render_to_response('newcoming.htm', render_para) #else session found return HttpResponse('hi')
class auth: def GET(self, hash): session = Session() try: authen = session.query(Authen).filter_by(hash=hash).one() except: return "hash %s not exists or expired" %hash try: user = session.query(User).filter_by(jid=authen.jid).one() except exc.NoResultFound, e: user = False return "user not found" if user and user.auth is True: return "user: %s jid: %s was already authenticated" %(user.uid, user.jid) client = OAuthClient(key=config.API_KEY, secret=config.API_SECRET) request_key, request_secret = client.get_request_token() if request_key and request_secret: try: token = Token(request_key, request_secret, hash) session.add(token) session.commit() #except: # return web.internalerror() finally: session.close() url = client.get_authorization_url(request_key, request_secret, callback=config.AUTH_CALLBACK) return web.TempRedirect(url) else: return "request request_key failed"
def GET(self): client = OAuthClient(key=config.API_KEY, secret=config.API_SECRET) request_key = web.input().get('oauth_token', False) session = Session() if request_key: try: token = session.query(Token).filter_by(key=request_key).one() except exc.NoResultFound, e: return "request token: %s not found" %request_key
else: return "request request_key failed" class callback: def GET(self): client = OAuthClient(key=config.API_KEY, secret=config.API_SECRET) request_key = web.input().get('oauth_token', False) session = Session() if request_key: try: token = session.query(Token).filter_by(key=request_key).one() except exc.NoResultFound, e: return "request token: %s not found" %request_key else: return web.internalerror() client = OAuthClient(key=config.API_KEY, secret=config.API_SECRET) access_key, access_secret = client.get_access_token(request_key, token.secret) if not access_key or not access_secret: return "Error: access_key: %s access_token: %s" %(access_key, access_secret) print "access_key %s, access_secret %s" %(access_key, access_secret) service = DoubanService(api_key=config.API_KEY, secret=config.API_SECRET) if not service.ProgrammaticLogin(access_key, access_secret): return "service login failed" try: people = service.GetAuthorizedUID('/people/@me') except: return web.internalerror() try:
def GET(self): sid = web.input().get('sid', '') if not sid: print 'no sid' return client = OAuthClient(key=API_KEY, secret=SECRET) cookies = web.cookies() access_key = cookies.get('access_key') access_secret = access_tokens.get(access_key) if not access_key or not access_secret: request_key = web.input().get('oauth_token', '') request_secret = request_tokens.get(request_key) if request_key and request_secret: try: access_key, access_secret = \ client.get_access_token(request_key, request_secret) if access_key and access_secret: # store user access key in cookie, # not accessable by other people web.setcookie('access_key', access_key) access_tokens[access_key] = access_secret except Exception: access_token = None print '获取用户授权失败' return else: client = OAuthClient(key=API_KEY, secret=SECRET) key, secret = client.get_request_token() if key and secret: request_tokens[key] = secret url = client.get_authorization_url( key, secret, callback=HOST + PREFIX + '/collection?sid=' + sid) web.tempredirect(url) return else: print '获取 Request Token 失败' return service = DoubanService(api_key=API_KEY, secret=SECRET) movie = service.GetMovie(sid) html_header() search_panel() print '<h2>你希望收藏电影: %s</h2>' % (movie.title.text) print '<div class="obss" style="margin-top:20px">' print '<dl class="obs"><dt>' print '<a href="%s" title="%s"><img src="%s" class="m_sub_img"/></a>' % ( movie.GetAlternateLink().href, movie.title.text, movie.link[2].href) print '</dt><dd>' print '<a href="%s">%s</a>' % (movie.GetAlternateLink().href, movie.title.text) print '</dd>' print '</dl>' if access_key and access_secret: if service.ProgrammaticLogin(access_key, access_secret): try: entry = service.AddCollection('wish', movie, tag=['test']) if entry: print '<span>已添加到你的收藏</span>' else: print '<span>添加收藏失败</span>' except Exception: print '<span>添加收藏失败, 授权失效</span>' del access_tokens[access_key] web.setcookie('access_key', '', 0) else: print '<span>无法添加收藏,可能你因为你没有授权这个应用访问你在豆瓣的数据</span>' print '</div>' html_footer()
def GET(self): sid = web.input().get('sid','') if not sid: print 'no sid' return client = OAuthClient(key=API_KEY, secret=SECRET) cookies = web.cookies() access_key = cookies.get('access_key') access_secret = access_tokens.get(access_key) if not access_key or not access_secret: request_key = web.input().get('oauth_token','') request_secret = request_tokens.get(request_key) if request_key and request_secret: try: access_key, access_secret = \ client.get_access_token(request_key, request_secret) if access_key and access_secret: # store user access key in cookie, # not accessable by other people web.setcookie('access_key', access_key) access_tokens[access_key] = access_secret except Exception: access_token = None print '获取用户授权失败' return else: client = OAuthClient(key=API_KEY, secret=SECRET) key, secret = client.get_request_token() if key and secret: request_tokens[key] = secret url = client.get_authorization_url(key, secret, callback=HOST+PREFIX+'/collection?sid='+sid) web.tempredirect(url) return else: print '获取 Request Token 失败' return service = DoubanService(api_key=API_KEY, secret=SECRET) movie = service.GetMovie(sid) html_header() search_panel() print '<h2>你希望收藏电影: %s</h2>' % (movie.title.text) print '<div class="obss" style="margin-top:20px">' print '<dl class="obs"><dt>' print '<a href="%s" title="%s"><img src="%s" class="m_sub_img"/></a>' % (movie.GetAlternateLink().href, movie.title.text, movie.link[2].href) print '</dt><dd>' print '<a href="%s">%s</a>' % (movie.GetAlternateLink().href, movie.title.text) print '</dd>' print '</dl>' if access_key and access_secret: if service.ProgrammaticLogin(access_key, access_secret): try: entry = service.AddCollection('wish', movie, tag=['test']) if entry: print '<span>已添加到你的收藏</span>' else: print '<span>添加收藏失败</span>' except Exception: print '<span>添加收藏失败, 授权失效</span>' del access_tokens[access_key] web.setcookie('access_key', '', 0) else: print '<span>无法添加收藏,可能你因为你没有授权这个应用访问你在豆瓣的数据</span>' print '</div>' html_footer()