Exemple #1
0
def callback():
    client = APIClient(app_key=APP_KEY,
                       app_secret=APP_SECRET,
                       redirect_uri=CALLBACK_URL)
    r = client.request_access_token(request.args['code'])
    ACCESS_TOKEN = r.access_token  # access token,e.g., abc123xyz456
    expires_in = r.expires_in  # token expires in
    client.set_access_token(ACCESS_TOKEN, expires_in)

    # TODO: ACCESS_TOKEN 存到文件
    print ACCESS_TOKEN
    with open("./application/data/weibo_token.txt", 'w') as fi:
        fi.write(ACCESS_TOKEN)

#    test
#    print client.statuses.user_timeline.get()
#    print client.statuses.update.post(status=u'test plain weibo')

#    client.statuses.update.post(status=u'test plain weibo 2')

    return redirect("/")
Exemple #2
0
def weibo(cmd=None, param=None):
    if cmd == "None" and param == "[]":
        ret = ("sendweibo:" + "</br>" + tab +
               "explain: send a weibo message!" + "</br>" + tab +
               "args1: the content want to post" + "</br>" + "getweibo" +
               "</br>" + tab + "expain: get weibo message of all friends" +
               "</br>" + tab +
               "args1: the index of page for all weibo messages" + "</br>" +
               tab + "args2: the number of weibo message view")
        ret = {
            "action": "output",
            "type": "html",
            "data": json.dumps(ret),
        }
        return json.dumps(ret)
    elif cmd == "getweibo":
        with open("./application/data/weibo_token.json", 'r') as fi:
            param = json.loads(param)
            page = "1"
            count = "20"
            if len(param) == 1:
                page = param[0]
            elif len(param) == 2:
                page = param[0]
                count = param[1]

            token = json.load(fi)
            url = 'https://api.weibo.com/2/statuses/friends_timeline.json'
            access_token = token[0]['access_token']
            data = "?access_token=" + access_token
            data += "&page=" + page
            data += "&count=" + count
            url += data
            ret = urllib2.urlopen(url)
            ret = json.load(ret)
            content = "<ul>"
            for w in ret.get('statuses'):
                content += "<li>" + w.get('user').get(
                    'screen_name') + "</li>" + "<li>" + w.get(
                        'text') + "</li>" + "</br>"
            content += "</ul>"
            ret = {}

            ret['action'] = 'output'
            ret['type'] = 'html'
            ret['data'] = content
            return json.dumps(ret)
    elif cmd == "sendweibo":
        with open("./application/data/weibo_token.json", 'r') as fi:
            token = json.load(fi)
            url = 'https://api.weibo.com/2/statuses/update.json'
            access_token = token[0]['access_token']
            data = {
                "status": str(param).strip('[').strip(']'),
                "access_token": access_token,
            }
            requests.post(url, data=data)
            ret = {"action": "output", "type": "text", "data": "success"}
            return json.dumps(ret)
    elif cmd == 'login':
        client = APIClient(app_key=APP_KEY,
                           app_secret=APP_SECRET,
                           redirect_uri=CALLBACK_URL)
        url = client.get_authorize_url()
        ret = {
            "action": "needauth",
            "type": "text",
            "data": url,
        }
        return json.dumps(ret)

    #url = ('https://api.weibo.com/oauth2/authorize?client_id='+
    #        '1220535963' +
    #        '&response_type=code&redirect_uri=' +
    #        'http://atupal.org')
    url = ('https://api.weibo.com/oauth2/authorize?client_id=' + '1220535963' +
           '&response_type=code&redirect_uri=' + 'http://atupal.org')
    ret = {
        "action": "output",
        "type": "text",
        "data": "no such command",
    }
    return json.dumps(ret)