웨이보api 인터페이스 로그인, 정보 얻기, 웨이보 공유

17034 단어
import json
from datetime import datetime

import MySQLdb
import requests
from flask import Flask, redirect, request

app = Flask(__name__)


class ApiError(Exception):
    def __init__(self, code, msg):
        super(ApiError, self).__init__()
        self.code = code
        self.msg = msg

    def __str__(self):
        return '{0}:{1}'.format(self.code, self.msg)

class ServerError(Exception):
    pass


class WeiboClient(object):

    API_URL = 'https://api.weibo.com/'

    def __init__(self, client_id, client_secret):
        self.client_id = client_id
        self.client_secret = client_secret
        self.token = {}

    @property
    def access_token(self):
        if self.token:
            return self.token['access_token']
        return None

    def fetch(self, method, url, params={}):
        '''
                 
        :param method:
        :param url:
        :param params:
        :return:
        '''
        try:
            if method == 'POST':
                resp = requests.post(url, params)
            else:
                resp = requests.get(url, params)

            if resp.status_code >= 200 and resp.status_code < 300:
                #     
                rest = resp.json()
                if 'error_code' in rest:
                    raise ApiError(rest['error_code'], rest['error'])
                return rest
            elif resp.status_code >= 400:
                raise ServerError()
        except ApiError as e:
            print('ApiError')
            pass
        except ServerError as e:
            print('ServerError')
        except Exception:
            print('Exception')

    def get_ticket_url(self, redirect_uri=None):
        '''
                  url
        :param redirect_uri:
        :return:
        '''
        if redirect_uri is None:
            redirect_uri = 'http://test.baidu.com'
        url = self.API_URL + 'oauth2/authorize?client_id={0}&response_type=code&redirect_uri={1}'.format(
            self.client_id,
            redirect_uri
        )
        # get  
        return url

    def get_token(self, code):
        '''
          token
        :param code:
        :return:
        '''
        #
        if self.token:
            return self.token
        url = self.API_URL + 'oauth2/access_token?client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri=http://test.baidu.com&code={2}'.format(
            self.client_id,
            self.client_secret,
            code
        )
        resp = self.fetch('POST', url)
        self.token = resp.json()
        return self.token

    def get_user_info(self, access_token, uid):
        '''
              
        :param code:
        :param uid:
        :return:
        '''
        url = self.API_URL + '2/users/show.json'
        # access_token = self.get_token(code)['access_token']
        resp = self.fetch('GET', url, {
            'access_token': access_token,
            'uid': uid
        })
        return resp.json()

    def get_conn(self):
        """   mysql     """
        try:
            conn = MySQLdb.connect(
                db='db_user',
                host='localhost',
                user='root',
                password='',
                charset='utf8'
            )
        except:
            pass
        return conn

    def weibo_share(self):
        '''
               
        :param access_token:
        :return:
        '''

        url = self.API_URL + '2/statuses/share.json'
        resp = self.fetch('POST', url, {
            # 'access_token': self.access_token,
            'status': '       : {0} http://test.baidu.com'.format(datetime.now())
        })
        return resp


client_id = '3xxxx3'
client_secret = '0b4axxxx'
client = WeiboClient(client_id, client_secret)

@app.route('/')
def index():
    code = request.args.get('code', None)
    #   code   token
    token = client.get_token(code)
    #       

    user_info = client.get_user_info(token['access_token'], token['uid'])
    third_id = user_info['id']
    nickname = user_info['screen_name']
    headimg = user_info['profile_image_url']

    #         
    conn = client.get_conn()
    cursor = conn.cursor()
    sql = "INSERT INTO `user`(`third_id`, `nickname`, `headimg`) VALUES('{third_id}', '{nickname}', '{headimg}')".format(
        third_id=third_id, nickname=nickname, headimg=headimg)
    print(sql)
    cursor.execute(sql)
    conn.autocommit(True)
    return json.dumps(user_info)


@app.route('/weibo')
def weibo():
    ticket = client.get_ticket_url()
    return redirect(ticket)


@app.route('/share')
def share():
    rest = client.weibo_share()
    return json.dumps(rest)


if __name__ == '__main__':
    app.run(debug=True, port=80)

 
전재 대상:https://www.cnblogs.com/Erick-L/p/11180855.html

좋은 웹페이지 즐겨찾기