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()
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(): 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()
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"))