def authTencentUrl(): auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite + "/tencentcallback") url = auth.get_authorization_url() f = open('tx_rt', 'wb') f.write(pickle.dumps(auth.request_token)) f.close() return url
def authTencent(verifier, token): auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite+"/tencentcallback") auth.request_token = pickle.loads(memcache.get(key="tx_rt")) logging.info(auth.request_token) access_token = auth.get_access_token(verifier) tencentData = weiboDataModel(source="tencent") tencentData.access_token_secret=access_token.secret tencentData.access_token=access_token.key tencentData.put() return True
def tencentcallback(request): verifier = request.GET['oauth_verifier'] auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite + "/tencentcallback") f = open('tx_rt', 'rb') auth.request_token = pickle.loads(f.read()) f.close() access_token = auth.get_access_token(verifier) user_type = mc.get('user_type') mc.set('user_type', 'invalid') TencentData = WeiboAuth(access_token=access_token.key, access_token_secret=access_token.secret, source="tencent", expires_in=13000000, user_type=user_type) TencentData.save() return HttpResponse("auth info saved")
def getWeiboAuthedApi(source, user_type): try: weiboData = WeiboAuth.objects.get(source=source, user_type=user_type) except Exception as e: log.info("no valid auth info for %s, %s found" % (source, user_type)) raise e if source == 'sina': client = weibo.APIClient(SinaAppKey, SinaAppSecret, MoeWebsite + "/sinacallback") client.set_access_token(weiboData.access_token, weiboData.expires_in) return client if source == 'tencent': auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite + '/tencentcallback') auth.setToken(weiboData.access_token, weiboData.access_token_secret) api = qqweibo.API(auth, parser=qqweibo.ModelParser()) return api
def sendByTencent(title, link, picture): auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite+"/tencentcallback") tencentDatas = weiboDataModel.gql("where source='tencent'") if(tencentDatas.count()==0): logging.error("No existing tencent data found") return False flag = True for tencentData in tencentDatas: auth.setToken(tencentData.access_token, tencentData.access_token_secret) api = qqweibo.API(auth, parser=qqweibo.ModelParser()) try: if(picture==None): api.tweet.add(title+" "+link, clientip='127.0.0.1') else: wrapper = StringIO.StringIO(picture) api.tweet.addpic(wrapper, title+" "+link, clientip='127.0.0.1') wrapper.close() except Exception, e: flag = False logging.error(e)
def authTencentURL(): auth = qqweibo.OAuthHandler(TencentAppKey, TencentAppSecret, callback=MoeWebsite+"/tencentcallback") url = auth.get_authorization_url() memcache.set(key="tx_rt", value=pickle.dumps(auth.request_token)) return url
def get_client(): _, app_key, app_secret, access_token, token_secret = random.choice( APP_CREDENTIALS) auth_handler = qqweibo.OAuthHandler(app_key, app_secret) auth_handler.setToken(access_token, token_secret) return qqweibo.API(auth_handler)