class AlfredQQWeibo(): auth = OAuthHandler(API_KEY, API_SECRET) setting = {} def load_setting(self): with open('setting.pickle', 'rb') as f: self.setting = pickle.load(f) def save_setting(self): with open('setting.pickle', 'wb') as f: pickle.dump(self.setting, f) def verify(self): auth_url = self.auth.get_authorization_url() requestToken = self.auth.request_token.key requestTokenSecret = self.auth.request_token.secret if (requestToken != None and requestToken.count > 0): print "确认授权后,运行:\n \"qwb pin {授权码}\"" self.setting['request_token'] = requestToken self.setting['request_token_secret'] = requestTokenSecret self.save_setting() webbrowser.open(auth_url) else: print 'ERROR, try again pls.' def getAccessToken(self, pin): self.load_setting() self.auth.set_request_token(self.setting['request_token'], self.setting['request_token_secret']) access_token = self.auth.get_access_token(pin) if (access_token.key != None and access_token.secret != None and access_token.key.count > 0 and access_token.secret.count > 0): self.setting['access_token'] = access_token.key self.setting['access_token_secret'] = access_token.secret self.save_setting() print '验证成功.运行"qwb {message}",发送微博' else: print "ERROR, try again pls." def tweet(self, message): self.load_setting() self.auth.set_request_token(self.setting['request_token'], self.setting['request_token_secret']) self.auth.set_access_token(self.setting['access_token'], self.setting['access_token_secret']) api = API(self.auth, parser=ModelParser()) ret = api.tweet.add(message, clientip='127.0.0.1') if (ret): tw = api.tweet.show(ret.id) print('发送成功:微博ID={0.id}\n用户名={0.nick}\n内容={0.text}'.format(tw))
def qqauth(request): try: auth = OAuthHandler(settings.QQ_APP_KEY, settings.QQ_APP_SECRET, settings.QQ_CALLBACK) url = auth.get_authorization_url() request.session['qqauth'] = auth return HttpResponseRedirect(url) except Exception as e: logging.error(e) return HttpResponseRedirect('')
def get_api(): import sys, os.path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from qqweibo import OAuthHandler, API c = get_conf() try: o = OAuthHandler(c['consumer_key'], c['consumer_secret']) o.setToken(c['access_token_key'], c['access_token_secret']) except KeyError, e: sys.stderr.write("qq: you should run get_oauthed.py first.\n")
def main(): c = get_conf() o = OAuthHandler(c['consumer_key'], c['consumer_secret']) c['authorization_url'] = o.get_authorization_url() c['request_token'] = o.request_token print c['authorization_url'] verifier = raw_input('click the url above, then input the verifier: ') verifier = verifier.strip() c['verifier'] = verifier try: access_token = o.get_access_token(verifier) c['access_token_key'] = access_token.key c['access_token_secret'] = access_token.secret c['access_token'] = access_token.to_string() set_conf(c) print 'oauth succeeded' except: print 'oauth failed'
#! /usr/bin/env python #coding=utf-8 from config import * from qqweibo import OAuthHandler, API, JSONParser, ModelParser from utils import store_category, weibo_logger, store_error API_KEY = '801322882' API_SECRET = '0f4b0f472813b31c23720623014e22e9' a = OAuthHandler(API_KEY, API_SECRET) def get_token(): print a.get_authorization_url() verifier = raw_input('PIN: ').strip() print a.get_access_token(verifier) # or directly use: #token = 'e9fc735b76ba4e75a6ebaefe61ee66fc' token = 'd169abff38d747cfa8bdb21123577482' #tokenSecret = 'ec07bda1b332156d1554470893b16b6d' tokenSecret = '25d60b4c67c488a4d5291dcb9bd43fd8' a.setToken(token, tokenSecret) api = API(a) QQ_WEIBO_INFO_SOURCE_ID = 7