本文整理汇总了Python中weibo.APIClient类的典型用法代码示例。如果您正苦于以下问题:Python APIClient类的具体用法?Python APIClient怎么用?Python APIClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了APIClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: callback
def callback():
code = request.args.get('code')
if code is not None:
client = APIClient(
app_key = current_app.config['APP_KEY'],
app_secret = current_app.config['APP_SECRET'],
redirect_uri = current_app.config['CALLBACK_URL'])
r = client.request_access_token(code)
access_token = r.access_token
expires_in = r.expires_in
uid = r.uid
client.set_access_token(access_token, expires_in)
session['access_token'] = access_token
user = User.query.filter_by(uuid = uid).first()
if user is not None:
login_user(user)
return redirect(url_for('main.index'))
else:
u = client.users.show.get(uid = uid)
user = User(
uuid = uid,
name = u.get('screen_name'),
location = u.get('location'),
description = u.get('description'),
avatar = u.get('profile_image_url'),
url = u.get('domain'),
gender = u.get('gender')
)
db.session.add(user)
login_user(user)
return redirect(url_for('main.index'))
else:
return redirect(url_for('main.index'))
示例2: weibo_check
def weibo_check(request):
code = request.GET.get('code', None)
now = datetime.datetime.now()
if code:
client = APIClient(app_key=settings.WEIBO_APP_KEY, app_secret=settings.WEIBO_APP_SERCET, redirect_uri=settings.WEIBO_CALLBACK_URL)
r = client.request_access_token(code)
access_token = r.access_token # 返回的token,类似abc123xyz456
expires_in = r.expires_in # token过期的UNIX时间:http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4
uid = r.uid
# 在此可保存access token
client.set_access_token(access_token, expires_in)
request.session['access_token'] = access_token
request.session['expires_in'] = expires_in
request.session['uid'] = uid
# http://open.weibo.com/wiki/2/users/show
data=client.user.show.get(uid)
user=User(username=uid)
user.save()
name=data.get('name')
return HttpResponseRedirect('/verification/index/?username='+name)
# user = SupserWeibo(access_token=access_token, uid=uid, request=request) # 实例化超级微博类
'''
# 更新数据库
if MyUser.objects.filter(uid=uid).exists():
MyUser.objects.filter(uid=uid).update(last_login=now)
user.Login() # 登陆
return HttpResponseRedirect('/')#返回主页
else:
# 创建用户并登陆
u_id = user.createUser()
if u_id:
return HttpResponseRedirect('/manage/user/%s/' %u_id)
'''
return HttpResponse('/404/') #未获得新浪微博返回的CODE
示例3: get
def get(self):
# OAth2 for douban
KEY = conf['douban_app_key']
SECRET = conf['douban_app_secret']
CALLBACK = conf['url'] + '/dback'
SCOPE = 'douban_basic_common,community_basic_user,community_basic_note'
client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE)
douban_login = client.authorize_url
# OAth2 for sina_weibo
APP_KEY = conf['weibo_app_key']
APP_SECRET = conf['weibo_app_secret']
CALLBACK_URL = conf['url'] + '/wback'
weibo_client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
weibo_login = weibo_client.get_authorize_url()
# statistics
diary_count = Diary.get_diary_count()
last_diary = Diary.get_last_diary()
first_diary = Diary.get_first_diary()
comment_count = Comment.get_comment_count()
usr = tornado.escape.xhtml_escape(self.current_user)
site_start = Admin.find_by_username(usr).get('site_start')
self.render('Admin/dashboard.html', douban_login=douban_login,
diary_count=diary_count, last_diary=last_diary,
first_diary=first_diary, site_start=site_start,
comment_count=comment_count, weibo_login=weibo_login
)
示例4: auth_signin
def auth_signin():
'''
Redirect to sina sign in page.
'''
ctx.response.set_cookie(COOKIE_REDIRECT, _get_referer())
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK)
raise seeother(client.get_authorize_url())
示例5: __init__
class Weibo:
def __init__(self, api_info):
self.client = APIClient(app_key = api_info['app_key'], app_secret = api_info['app_secret'])
self.client.set_access_token(api_info['access_token'], api_info['expires_in'])
def get_user_weibo(self, username, page_no, count = 100):
return self.client.get.statuses__user_timeline(uid = username, count = count, page = page_no)
def create_weibo(self, text):
return self.client.post.statuses__update(status = text)
def create_weibo_with_pic(self, text, pic):
return self.client.upload.statuses__upload(status = text, pic = pic)
def repost_weibo(self, id, text, is_comment = 0):
return self.client.post.statuses__repost(id = id, status = text, is_comment = is_comment)
def follow_you(self, uid):
return self.friendships__create(uid = uid)
def comments_show(self, id):
''' list all the reply of one weibo '''
return self.client.get.comments__show(id = id)
def comments_create(self, id, text):
return self.client.post.comments__create(id = id, comment = text)
def comments_reply(self, id, cid, text):
return self.client.post.comments__reply(id = id , cid = cid, comment = text)
示例6: morefriends
def morefriends(request):
weibo_client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
if request.COOKIES.has_key('access_token') is False:
DEBUG_TRACE.debug("Has no acccess token")
return HttpResponseRedirect(CALLBACK_URL)
else:
access_token = request.COOKIES['access_token']
expires_in = request.COOKIES['expires_in']
uid = request.COOKIES['uid']
friend_cursor = request.GET['friend_cursor']
weibo_client.set_access_token(access_token, expires_in)
friend_info = weibo_client.get.friendships__friends(uid=uid, count=20, cursor=friend_cursor)
friend_list = getFriendList(friend_info)
t = Template("""
{% for friend in friend_list %}
<li><a href="photogallary?query_screen_name={{ friend.screen_name }}">
<img width="80" height="80" src="{{ friend.avatar_large }}"></img>
<h3>{{ friend.screen_name }}</h3>
<p>{{ friend.description }}</p>
</a></li>
{% endfor %} """)
c = Context({'friend_list': friend_list})
html = t.render(c)
response = HttpResponse(html)
return response
示例7: __init__
class Sina:
def __init__(self, app_key, app_secret, redirect_uri):
self.client = APIClient(app_key, app_secret, redirect_uri)
if not os.path.exists("./sinadb"):
self.request_url = self.client.get_authorize_url()
print self.request_url
code = raw_input('waiting code:')
f = open("./sinadb", 'w')
f.write(str(code))
r = self.client.request_access_token(code)
access_token = r.access_token
f.write(str(access_token))
expires_in = r.expires_in
f.write(str(expires_in))
f.close()
else:
f = open("./sinadb", 'r')
code = f.readline().replace("\n",'')
access_token = f.readline().replace("\n",'')
expires_in = f.readline().replace("\n",'')
self.client.set_access_token(access_token, expires_in)
def twite(self):
input_status=raw_input('STATUS: ')
if input_status == "quit":
return -1
pic_path=raw_input('PICTURE: ')
if not pic_path:
self.client.post.statuses__update(status=input_status)
else:
self.client.upload.statuses__upload(status=input_status, pic=open(pic_path))
示例8: event_func
def event_func():
APP_KEY='2218208544'
APP_SECRET='956ae245a2f62a11a9eca1d91fbda092'
CALLBACK_URL='http://cdgir.cumt.edu.cn/ShowCode.aspx'
client=APIClient(app_key=APP_KEY,app_secret=APP_SECRET,\
redirect_uri=CALLBACK_URL)
url=client.get_authorize_url()
print url
webbrowser.open(url)
code=raw_input("input the code:").strip()
print code
r=client.request_access_token(code);
access_token=r.access_token
expires_in=r.expires_in
client.set_access_token(access_token,expires_in)
val=client.place.pois.show.get(poiid='B2094757D06FA3FD4499')
text="%s\t%s\t%s\t%s\r\n" % (val.title,val.lon,val.lat,val.address)
#print poi_result.total_number
#for val in poi_result:
#for k, v in val.items():
#print ("%s : %s" % (k,v))这个是val这个dic的循环遍历
#text+="%s\t%s\t%s\t%s\r\n" % (val.title,val.lon,val.lat,val.address)#可参考api的json返回示例
print text
#print text.encode('utf-8')
open('log.txt','a').write(text.encode('utf-8'))
示例9: GET
def GET(self):
try:
ins=web.input()
# 获取URL参数code:
code = ins.get('code')
client = APIClient(app_key=CONSUME_KEY, app_secret=CONSUME_SECRET,redirect_uri=web.ctx.get('homedomain')+'/callback')
r = client.request_access_token(code)
access_token = r.access_token # 新浪返回的token,类似abc123xyz456
#return str(access_token )
expires_in = r.expires_in # token过期的UNIX时间:http://zh.wikipedia.org/wiki/UNIX%E6%97%B6%E9%97%B4
# TODO: 在此可保存access token
client.set_access_token(access_token, expires_in)
#oauth_verifier=ins.get('oauth_verifier',None)
#request_token=session.get('request_token',None)
#auth=OAuthHandler(CONSUME_KEY, CONSUME_SECRET)
#auth.request_token=request_token
#通过oauth_verifier来获取access_token
#access_token=auth.get_access_token(oauth_verifier)
session.access_token=access_token
session.code = code
session.expires_in = expires_in
#return str(access_token)
print '111111111111111111111'
#user = client.get.users_show()
#id = client.get.account__get_uid()
#return str(expires_in)
web.seeother("/index")
except Exception:
web.header("Content-Type", "text/html;charset=utf-8")
return ':-( 出错了在这里222'+ str(code)
示例10: Weibo
class Weibo(object):
def __init__(self):
self.APP_KEY = current_app.config['WEIBO_APP_KEY']
self.APP_SECRET = current_app.config['WEIBO_APP_SECRET']
self.CALLBACK_URL = current_app.config['WEIBO_CALLBACK_URL']
self.ACCESS_TOKEN = current_app.config['WEIBO_ACCESS_TOKEN']
self.EXPIRES_TIME = current_app.config['WEIBO_EXPIRES_TIME']
self.client = APIClient(
app_key=self.APP_KEY, app_secret=self.APP_SECRET, redirect_uri=self.CALLBACK_URL)
self.client.set_access_token(self.ACCESS_TOKEN, self.EXPIRES_TIME)
def post_weibo(self, **kw):
current_app.logger.info('_post_weibo')
try:
body = kw.get('body')
url = kw.get('url')
nickname = u' 团长: ' + kw.get('nickname')
topic = u'#海淘拼单#'
r = self.client.statuses.share.post(
status=body + nickname + topic + url)
return r
except Exception as e:
current_app.logger.exception('_post_weibo')
sendException(e, 'Weibo post_weibo')
return None
示例11: callback
def callback(request):
code = request.GET.get('code')
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
r = client.request_access_token(code)
request.session['token'] = r.access_token
request.session['expire'] = r.expires_in
return HttpResponseRedirect('/')
示例12: GetCode
def GetCode(userid, passwd):
"""获取用户code"""
print 'Get the code...',
client = APIClient(app_key = APP_KEY, app_secret = APP_SECRET, redirect_uri = CALLBACK_URL)
referer_url = client.get_authorize_url()
postdata = {
"action": "login",
"client_id": APP_KEY,
"redirect_uri":CALLBACK_URL,
"userId": userid,
"passwd": passwd,
}
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0",
"Referer":referer_url,
"Connection":"keep-alive"
}
req = urllib2.Request(
url = AUTH_URL,
data = urllib.urlencode(postdata),
headers = headers
)
resp = urllib2.urlopen(req)
print 'success!!!'
return resp.geturl()[-32:]
示例13: request_access_token
def request_access_token(code, oauth, redirect_uri=None):
'''
return access token as object: {"access_token":"your-access-token","expires_in":12345678,"uid":1234}, expires_in is standard unix-epoch-time
'''
client = APIClient(apitype='oauth2', format=None)
redirect = redirect_uri if redirect_uri else oauth.app_callback
if not redirect:
raise APIError('21305', 'Parameter absent: redirect_uri', 'OAuth2 request')
r = client.access_token(
client_id=oauth.app_key,
client_secret=oauth.app_secret,
redirect_uri=redirect,
code=code, grant_type='authorization_code',
_method='POST'
)
current = int(time.time())
expires = r.expires_in + current
remind_in = r.get('remind_in', None)
if remind_in:
rtime = int(remind_in) + current
if rtime < expires:
expires = rtime
jo = JsonObject(access_token=r.access_token, expires_in=expires)
uid = r.get('uid', None)
if uid:
jo.uid = uid
return jo
示例14: weibo_auth
def weibo_auth(request):
# 获取URL参数code:
code = request.GET.get('code')
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=_get_weibo_callback_url(request))
token_obj = client.request_access_token(code)
client.set_access_token(token_obj.access_token, token_obj.expires_in)
if request.session.has_key('oauth_access_token'):
del request.session['oauth_access_token']
request.session['oauth_access_token'] = { 'uid' : token_obj.uid, 'access_token' : token_obj.access_token, 'expires_in' : token_obj.expires_in}
oauth_access_token = request.session.get('oauth_access_token', None)
back_to_url = reverse('songs.views.my_home')
if token_obj is not None:
try:
w_user = WeiboUser.objects.get(weibo_user_id=oauth_access_token['uid'])
user = authenticate(weibo_user=w_user)
if user and user.is_active:
auth_login(request,user)
except WeiboUser.DoesNotExist:
back_to_url = reverse('social.views.create_user_from_weibo')
return HttpResponseRedirect(back_to_url)
示例15: morepictures
def morepictures(request):
weibo_client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
if request.COOKIES.has_key('access_token') is False:
DEBUG_TRACE.debug("Has no acccess token")
return HttpResponseRedirect(CALLBACK_URL)
else:
access_token = request.COOKIES['access_token']
expires_in = request.COOKIES['expires_in']
uid = request.COOKIES['uid']
page = request.GET['page']
name = request.GET['name']
weibo_client.set_access_token(access_token, expires_in)
user_info = weibo_client.get.users__show(screen_name=name)
screen_name = user_info.screen_name
avatar_large = user_info.avatar_large
photo_list = getPhotoList(weibo_client, screen_name, 5, page)
t = Template("""
{% for photo in photo_list %}
<li data-icon="false"><a href="">
<div class="my-li-thumb">
<img width="50" height="50" src="{{ avatar_large }}" />
</div>
<h3 class="my-li-name">{{ screen_name }}</h3>
<p class="my-li-text">{{ photo.text }}</p>
<div class="my-li-content">
<img class="my-li-content-image" src="{{ photo.image }}" />
</div>
</a></li>
{% endfor %}""")
c = Context({'photo_list': photo_list, 'screen_name': screen_name, 'avatar_large': avatar_large})
html = t.render(c)
response = HttpResponse(html)
return response