【重要】新浪微博api研究

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#新浪微博api研究

'''
3.SDK的使用规则:
    1)使用微博API,需要通过用户的授权,获取用户的授权码code;
    2)该SDK调用API的方法名称映射规则有以下几种,以API中的statuses/public_timeline接口为例:
        a)client.get.statuses__public_timeline(),将"/"换为两个"_",注意是两个。同时根据请求是POST还是GET,使用get或者post;
        b)client.statuses__public_timeline(),对于GET方式,在调用API时,可以将get省略;
        c)client.statuses.public_timeline.get(),将API中的"/"更换为".",同时最后指定是get方式还是post方式。
        
    3)参数问题:如果API中有参数指定,则在方法中提供参数;
    4)返回值:新浪返回的结果是JSON,但是SDK将其封装成了JSON对象,直接通过返回结果调用相应的属性即可。
'''
import os
import sys
import weibo
import webbrowser
import json,urllib,urllib2


APP_KEY='xxxxxx'
APP_SECRET='xxxxxxxxxxxxxxxxxxx'
CALLBACK_URL='http://www.cnblogs.com/dengyg200891'#这个是设置回调地址,必须与那个”高级信息“里的一致


#请求用户授权的过程
client = weibo.APIClient(APP_KEY, APP_SECRET,CALLBACK_URL)
#print client


authorize_url = client.get_authorize_url(CALLBACK_URL)
'https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//www.cnblogs.com/dengyg200891&response_type=code&client_id=3707867442'



#打开浏览器,需手动找到地址栏中URL里的code字段
webbrowser.open(authorize_url)


#进入到授权页面,如下图,请点击‘授权’——在浏览器地址中可查看到code字段所对应的值。
#code会随时变化,用一段时间发现无效了请及时更换。
#code='08ae262f84ae74fc3dfdc02f98ac12f3'
code=raw_input('请输入code:')



#获得用户授权
r = client.request_access_token(code)
print r
#{'access_token': u'2.00PxVPDEahpvCE5d8093b5a508MXEa', 'expires': 1603888639, 'expires_in': 1603888639, 'uid': u'3712558093'}



#access_token
access_token=r.access_token
expires=r.expires
client.set_access_token(access_token, expires)



#设置accsess_token,client可以直接调用API了
#发消息
#client.statuses.update.post(status='通过Python SDK发微博')



#设置accsess_token,client可以直接调用API了
#获取@当前用户的最新微博的ID
statuses = client.statuses__mentions__ids()
print statuses
'''
{'interval': 0,
 'hasvisible': False, 
'total_number': 3, 
'previous_cursor': 0, 
'next_cursor': 0, 
'statuses': [u'3884210072698912', u'3853907086305751', u'3853671818993765']}
'''



#设置accsess_token,client可以直接调用API了
#发布一条新微博,可以实现自动发微博
#content='xiaodeng'
#data = {'access_token':access_token,'status':content}
#data=urllib.urlencode(data)
#reqUrl = "https://api.weibo.com/2/statuses/update.json"
#req = urllib2.Request(url = reqUrl,data =data)
#urllib2.urlopen(req,timeout=5)
print 'OK'



#设置accsess_token,client可以直接调用API了
#获取用户关注的用户UID列表,请注意不是粉丝而是关注
#该api有使用次数限制
#https://api.weibo.com/2/friendships/friends/ids.json
#get_results = client.friendships__friends__ids()
#print get_results
'''
{
    'total_number': 47,   
    'previous_cursor': 0, 
    'ids': [1949305184, 1741045432, 1904178193, 1751627082, 1761587065, 2678120877L, 1826792401, 1830008825, 1640328892, 5305630013L, 5608433557L, 5270199617L, 5098152832L, 2141823055], 
    'next_cursor': 0
}
total_number:关注用户数量
ids:关乎用户的ID
'''

print 'OK'



#根据微博ID删除指定微博
#reqUrl = "https://api.weibo.com/2/statuses/destroy.json"
#data = {'access_token':access_token,'id':'3853907086305751'}  #id,写入微博的ID
#data=urllib.urlencode(data)
#req = urllib2.Request(url = reqUrl,data =data)
#urllib2.urlopen(req)
print 'OK'



#批量获取用户的粉丝数、关注数、微博数
#https://api.weibo.com/2/users/counts.json
#注意该url中间有一个2,这样的url一般可以用下面的方式来运用api。
get_results = client.users__counts(uids='1949305184') #1949305184是微博桌面
#print get_results
#[{'statuses_count': 643, 'friends_count': 297, 'private_friends_count': 0, 'followers_count': 2499195, 'pagefriends_count': 2, 'id': 1949305184}]

print 'OK'



#根据用户ID获取用户信息
#请求方式:get
#https://api.weibo.com/2/users/show.json
#注意该url中间有一个2,这样的url一般可以用下面的方式来运用api。
get_results = client.users__show(uid='1949305184')  #1949305184是微博桌面
#print get_results

print 'OK'



#获取某个用户最新发表的微博列表,默认查询最近发布的5篇微博,可以设置查询的篇数(注意查看参数列表的---count参数)
#此接口最多只返回最新的5条数据,官方移动SDK调用可返回10条;
#如果是需要查询其他微博好友,则需要添加参数来解决

#https://api.weibo.com/2/statuses/user_timeline.json
#注意该url中间有一个2,这样的url一般可以用下面的方式来运用api。
get_results = client.statuses__user_timeline(count=1)
#print get_results

print 'OK'
原文地址:https://www.cnblogs.com/dengyg200891/p/4924649.html