Пример #1
0
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')
Пример #2
0
            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:
            authen = session.query(Authen).filter_by(hash=token.hash).one()
Пример #3
0
    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()
Пример #4
0
    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()