def login(self): if self.cfg.access_token == '': self.client = APIClient( SinaWeiboMixin, self.cfg.key, self.cfg.secret, self.cfg.callback ) url = self.client.get_authorize_url() print url code = raw_input('code=?') r = self.client.request_access_token(code) self.cfg.access_token = self.client.access_token self.cfg.remind_in = self.client.remind_in self.cfg.expires_at = self.client.expires_at self.cfg.cfg.set('login', 'code', self.cfg.code ) self.cfg.cfg.set('login', 'access_token', self.cfg.access_token) self.cfg.cfg.set('login', 'remind_in' , self.cfg.remind_in ) self.cfg.cfg.set('login', 'expires_at' , self.cfg.expires_at ) self.cfg.cfg.write(open(self.cfg.cfg_file, 'w')) else: self.client = APIClient( SinaWeiboMixin, self.cfg.key, self.cfg.secret, self.cfg.callback, self.cfg.access_token, self.cfg.expires_at ) self.screen_name = self.client.users.show.get(uid=self.cfg.uid)['screen_name']
def get_newest_publicweibo(client): ''' get newest friend microblog ''' jsdict = {} try: jsdict = client.statuses.friends_timeline.get(page=1, count=180) except Exception as e: logging.error('>>>>>>>>>>get_newest_publicweibo ERROR : %s %s' % (datetime.now(), e)) print('>>>>GET ERROR') del client time.sleep(10) config_init() client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) url = client.get_authorize_url() # redirect the user to 'url' access_token = '2.00TD47_Gv7rSwBd7c70e9da30PlWbc' expires = 1614584749.0 client.set_access_token(access_token, expires) jsdict = get_newest_publicweibo(client) logging.error('>>>>>>>>>> %s' % jsdict) finally: return jsdict
def client_with_access_token(a): c = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL, access_token=a, expires=expires) return c
class MyWeibo: """ http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI """ def __init__(self, config='miniaxel.ini'): self.cfg = Config(config) def login(self): if self.cfg.access_token == '': self.client = APIClient( SinaWeiboMixin, self.cfg.key, self.cfg.secret, self.cfg.callback ) url = self.client.get_authorize_url() print url code = raw_input('code=?') r = self.client.request_access_token(code) self.cfg.access_token = self.client.access_token self.cfg.remind_in = self.client.remind_in self.cfg.expires_at = self.client.expires_at self.cfg.cfg.set('login', 'code', self.cfg.code ) self.cfg.cfg.set('login', 'access_token', self.cfg.access_token) self.cfg.cfg.set('login', 'remind_in' , self.cfg.remind_in ) self.cfg.cfg.set('login', 'expires_at' , self.cfg.expires_at ) self.cfg.cfg.write(open(self.cfg.cfg_file, 'w')) else: self.client = APIClient( SinaWeiboMixin, self.cfg.key, self.cfg.secret, self.cfg.callback, self.cfg.access_token, self.cfg.expires_at ) self.screen_name = self.client.users.show.get(uid=self.cfg.uid)['screen_name'] def get_friends_timeline(self): result = self.client.statuses.friends_timeline.get() return result def post(self, msg, pic_file=None): if pic_file: pic = open(pic_file) else: pic = None self.client.statuses.upload.post(status=msg, pic=pic)
def main(): FILE = os.curdir logging.basicConfig(filename=os.path.join(FILE, 'log.txt'), level=logging.INFO) config_init() print('APP_KEY:%s APP_SECRET:%s CALLBACK_URL:%s' % (APP_KEY, APP_SECRET, CALLBACK_URL)) client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) url = client.get_authorize_url() # redirect the user to 'url' print(url) # needGetCode = raw_input("Do you need to get code from callback in browser? Please input yes or no!") needGetCode = 'no' code = '' r = {} if needGetCode == 'yes': code = raw_input("Please input the returned code : " ) # redirect to url and get code r = client.request_access_token(code) print(r.access_token) # access token,e.g., abc123xyz456 print( r.expires ) # token expires time, UNIX timestamp, e.g., 1384826449.252 (10:01 am, 19 Nov 2013, UTC+8:00) # 测试版本access_token的时间期限为1天,过期后再重新获取 access_token = '2.00TD47_Gv7rSwB6b1fbd6797YonR5C' expires = 1622967755.0 client.set_access_token(access_token, expires) # After Initialize API Authorization... get_blog_and_process(client) print('get_blog_and_process is over...')
def main(): import argparse parser = argparse.ArgumentParser(description="初始化APIClient对象") parser.add_argument("-key", "--key", type=str,\ help="input the APP_KEY", default="4123398440") parser.add_argument("-secret", "--secret", type=str,\ help="input the APP_SECRET", default="104ece76059afa20bca62aa5d9554704") parser.add_argument("-url", "--url", type=str,\ help="input the CALLBACK_URL", default="http://127.0.0.1/callback") parser.add_argument("-token", "--token", type=str,\ help="input the ACCESS_TOKEN", default="2.00hYRkpC9S3DVE203bceb0140FaZub") group_authcode_token = parser.add_mutually_exclusive_group(required=False) group_authcode_token.add_argument("-request_authcode", "--request_authcode", type=bool,\ help="whether make a request to access CALLBACK_URL to get the code, True or False?", default=False) group_authcode_token.add_argument("-request_token", "--request_token", type=bool,\ help="whether make a request to get the new ACCESS_TOKEN, True or False?", default=False) parser.add_argument("-code", "--code", type=str,\ help="code used to get the new ACCESS_TOKEN") args = parser.parse_args() APP_KEY = args.key APP_SECRET = args.secret CALLBACK_URL = args.url ACCESS_TOKEN = args.token from snspy import APIClient, SinaWeiboMixin if args.request_authcode: client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) url = client.get_authorize_url() print "the authorize url: %s used to get the authcode" % url elif args.request_token and args.code: client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) r = client.request_access_token(args.code) print "the new access token:%s" % r else: client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL, access_token=ACCESS_TOKEN) print client._mixin.__dict__ print client.statuses.mentions.get()
def main(): import argparse parser = argparse.ArgumentParser(description="初始化APIClient对象") parser.add_argument("-key", "--key", type=str,\ help="input the APP_KEY", default="4123398440") parser.add_argument("-secret", "--secret", type=str,\ help="input the APP_SECRET", default="104ece76059afa20bca62aa5d9554704") parser.add_argument("-url", "--url", type=str,\ help="input the CALLBACK_URL", default="http://127.0.0.1/callback") parser.add_argument("-token", "--token", type=str,\ help="input the ACCESS_TOKEN", default="2.00hYRkpC9S3DVE203bceb0140FaZub") group_authcode_token = parser.add_mutually_exclusive_group(required=False) group_authcode_token.add_argument("-request_authcode", "--request_authcode", type=bool,\ help="whether make a request to access CALLBACK_URL to get the code, True or False?", default=False) group_authcode_token.add_argument("-request_token", "--request_token", type=bool,\ help="whether make a request to get the new ACCESS_TOKEN, True or False?", default=False) parser.add_argument("-code", "--code", type=str,\ help="code used to get the new ACCESS_TOKEN") args = parser.parse_args() APP_KEY = args.key APP_SECRET = args.secret CALLBACK_URL = args.url ACCESS_TOKEN = args.token from snspy import APIClient,SinaWeiboMixin if args.request_authcode: client = APIClient(SinaWeiboMixin,app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL) url = client.get_authorize_url() print "the authorize url: %s used to get the authcode" % url elif args.request_token and args.code: client = APIClient(SinaWeiboMixin,app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL) r = client.request_access_token(args.code) print "the new access token:%s" % r else: client = APIClient(SinaWeiboMixin,app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL,access_token=ACCESS_TOKEN) print client._mixin.__dict__ print client.statuses.mentions.get()
def create_client(): client = APIC(WbM, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) client.set_access_token(ACCESS_TOKEN, EXPIRES) return client
from snspy import APIClient from snspy import SinaWeiboMixin import ConfigParser config = ConfigParser.ConfigParser() config.read('config.ini') APP_KEY = config.get("sinawb", 'APP_KEY') # app key APP_SECRET = config.get('sinawb', 'APP_SECRET') # app secret CALLBACK_URL = config.get('sinawb', 'CALLBACK_URL') # callback url client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL) print client.get_authorize_url() code = raw_input('Enter code >') r = client.request_access_token(code) config.set('sinawb', 'code', code) config.set('sinawb', 'access_token', r.access_token) config.set('sinawb', 'expires', r.expires) config.write(open("config.ini", "w"))
config = ConfigParser.ConfigParser() config.read('config.ini') # connect to mongodb hostname = config.get('mongodb', 'hostname') port = int(config.get('mongodb', 'port')) connection = Connection(hostname, port) db = connection.weibodb # weibodb database collection = db.favorites # favorites collection # OAUTH APP_KEY = config.get("sinawb", 'APP_KEY') # app key APP_SECRET = config.get('sinawb', 'APP_SECRET') # app secret CALLBACK_URL = config.get('sinawb', 'CALLBACK_URL') # callback url access_token = config.get('sinawb', 'access_token') expires = (int)(config.get('sinawb', 'expires')) client = APIClient(SinaWeiboMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL, access_token=access_token, expires=expires) # grab your favorite weibo start_page = int(config.get('sinawb', 'fav_start_page')) total_page = int(config.get('sinawb', 'fav_total_page')) fav_count_per_page = int(config.get('sinawb', 'fav_count_per_page')) time_sleep = int(config.get('sinawb', 'time_sleep')) query_mid_type = int(config.get('sinawb', 'query_mid_type')) UT.grabFavorites(client, collection, start_page, total_page, fav_count_per_page, time_sleep, query_mid_type)
# -*- coding:UTF-8 -*- __author__ = 'gancj' __data__ = '2015-06-18 18:51' __mail__ = '[email protected]/[email protected]' from snspy import APIClient from snspy import TwitterMixin # suppose you are using Twitter APP_KEY = '885594759' APP_SECRET = '2e1e21ee3d40d8267dc675de31fbf5e4' # CALL_BACK = 'http://bingbingrobot.sinaapp.com/' CALL_BACK = 'https://api.weibo.com/oauth2/default.html' client = APIClient(TwitterMixin, app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)