def sinaWeiBo(appKey,appSecret,infoDb,user,content): access_token=infoDb.get(user,None) if not access_token: client = APIClient(app_key=appKey, app_secret=appSecret) # 用appKey创建一个client request_token = client.get_request_token() # 1.app认证,如果认证成功,有sina返回一个request_token url="http://api.t.sina.com.cn/oauth/authorize?oauth_token=%s"%(request_token.oauth_token) # 需要用户对该app进行授权,这样保证app不会获取用户的帐号和密码 webbrowser.open(url) oauth_verifier = raw_input('PIN: ').strip() # 验证用户是否对app授权 即2.授权认证 request_token = OAuthToken(request_token.oauth_token, request_token.oauth_token_secret, oauth_verifier) # 构造新一个request_token client = APIClient(app_key=appKey, app_secret=appSecret, token=request_token) access_token = client.get_access_token() # 向sina获取access_token,即3.代理访问认证 infoDb.setdefault(user,access_token) client = APIClient(app_key=appKey, app_secret=appSecret, token=access_token) client.post.statuses__update(status=content.decode('utf-8').encode('utf-8')) for object in client.get.statuses__user_timeline(): # 获取该user发布的前20条微博 print "" # id = object["id"] text = object["text"] created_at = object["created_at"] print str(created_at)+":"+ text.encode('gb18030'), if 'retweeted_status' in object: object=object['retweeted_status'] print "retweeted:",object['text'].encode('gb18030'),
def get_user_info_v1(uid, oauth_token, oauth_token_secret): weiboClientV1 = APIClient(app_key, app_secret, OAuthToken(oauth_token, oauth_token_secret)) user = None try: user = weiboClientV1.post.users__show(source=app_key, user_id=uid) except Exception, e: print e
def FollowMe(number, logger): followNum = 0 for task in FollowMeTask.objects.filter(type=1, status=0)[:number * 2]: try: if followNum > number: break try: user = Access.objects.get(uid=task.uid) except: logger.error("not find access info for %d", task.uid) continue logger.debug("user %d" % user.uid) if user.version == 1: client = APIClientV1(BGApp.wdj_app_key, BGApp.wdj_app_secret, OAuthToken(user.data1, user.data2)) try: tuser = client.post.users__show(source=BGApp.wdj_app_key, user_id=user.uid) except Exception, e: print e logger.debug(traceback.format_exc()) logger.debug("time out access token") task.status = 2 task.save() time.sleep(1) continue client.post.friendships__create(source=BGApp.wdj_app_key, user_id=BGApp.dev_uid) task.status = 1 task.save() followNum += 1 elif user.version == 2: client = APIClientV2(BGApp.wdj_app_key, BGApp.wdj_app_secret) client.set_access_token(user.data1, time.time() + 90 * 24 * 3600) try: tuser = client.get.users__show(uid=user.uid) except: logger.debug(traceback.format_exc()) traceback.print_exc() logger.debug("time out access token") time.sleep(1) task.status = 2 task.save() continue followed = FriendShip.Follow(client, BGApp.dev_uid) task.status = 1 followNum += 1 task.save() else: logger.debug("bad user version %d", user.version) task.status = 2 task.save() followNum += 1 continue time.sleep(60)
def unfollow_v1(oauth_token, oauth_token_secret, uid): weiboClientV1 = APIClientV1(app_key, app_secret, OAuthToken(oauth_token, oauth_token_secret)) successed = False try: weiboClientV1.post.friendships__destroy(source=app_key, user_id=uid) successed = True except Exception, e: print e
def is_follow_v1(oauth_token, oauth_token_secret, source_uid_, target_uid_): weiboClientV1 = APIClientV1(app_key, app_secret, OAuthToken(oauth_token, oauth_token_secret)) followed = False try: weiboClientV1.get.friendships__show(source=app_key, source_id=source_uid_, target_id=target_uid_) followed = ret.source.following except Exception, e: print e
def GetBlogClient(uname, passw): APP_KEY = u'XXXXXXXXXX' # app key APP_SECRET = u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # app secret #实例化APIClient client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET) #获取OAuth request token reqToken = client.get_request_token() #用户授权url auth_url = client.get_authorize_url(reqToken) post_data = urllib.urlencode({ "action": "submit", "forcelogin": "", "from": "", "oauth_callback": "http://api.weibo.com/oauth2/default.html", "oauth_token": reqToken.oauth_token, "passwd": passw, "regCallback": "", "ssoDoor": "", "userId": uname, "vdCheckflag": 1, "vsnval": "" }) mat = re.search( r'&oauth_verifier=(.+)', urllib2.urlopen( urllib2.Request("http://api.t.sina.com.cn/oauth/authorize", post_data, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1)', 'Referer': auth_url })).url) if mat: client = APIClient( APP_KEY, APP_SECRET, OAuthToken(reqToken.oauth_token, reqToken.oauth_token_secret, mat.group(1))) #返回APIClient return APIClient(APP_KEY, APP_SECRET, client.get_access_token()) else: raise Exception()