新浪微博接口 java_Java新浪微博新接口处理和注意事项

新浪微博新接口处理和注意事项

一、

新浪微博最新的主要接口介绍:

1.读取接口

1)https://api.weibo.com/2/statuses/public_timeline.json

获取最新的公共微博

2)https://api.weibo.com/2/statuses/user_timeline.json

获取某个用户最新发表的微博列表

3)https://api.weibo.com/2/statuses/mentions.json

获取最新的提到登录用户的微博列表,即@我的微博

4)https://api.weibo.com/2/statuses/mentions/ids.json

获取@当前用户的最新微博的ID

5)https://api.weibo.com/2/statuses/show.json

根据微博ID获取单条微博内容

2.写入接口

1)https://api.weibo.com/2/statuses/repost.json

转发一条微博

2)https://api.weibo.com/2/statuses/destroy.json

根据微博ID删除指定微博

3)https://api.weibo.com/2/statuses/update.json

发布一条新微博

4)https://upload.api.weibo.com/2/statuses/upload.json

上传图片并发布一条新微博

3.授权接口

1)https://api.weibo.com/oauth2/authorize

请求用户授权Token

2)https://api.weibo.com/oauth2/access_token

获取授权过的Access

Token

二、

新浪微博API测试工具

通过这个网址http://open.weibo.com/tools/console进入API测试工具界面,登录之后就可以对新浪提供的API进行测试。

三、

青岛二期城管微博对接处理

由于以前的SDK包中很多很多功能不再适用,所以开发了一个新的接口代码去处理与新浪那边的对接。

1.本次对接用到的接口

1)https://api.weibo.com/2/statuses/mentions.json获取@我的微博

2)https://api.weibo.com/2/statuses/mentions/ids.json获取@我的微博ids

3)https://api.weibo.com/2/statuses/repost.json转发一条微博

4)https://api.weibo.com/2/comments/create.json回复微博

2.对获取@我的微博的处理

由于现在新浪对获取@我的微博做限制,只返回授权用户的微博,非授权用户的微博不返回,所以需要获取到@我的微博的ids,然后对@我的微博进行一次转发之后,然后再通过那个获取@我的微博的API进行获取,这里面需要注意的是要对ids里面的id进行过滤,防止进行多次转发,然后就是转发之后会返回一个json数据,里面记录了转发之后新的id,需要把这个id进行记录,否则会造成多次转发。

3.新浪微博频次限制注意

由于新浪微博对API接口的访问次数进行了限制,所以在开发的过程中需要考虑到这方面的因素,自己接口每次轮询的时间应该适当加长一点,如果超过频次限制,新浪会阻止访问。

4.ACCESS_TOKEN值的判断和获取

新浪微博的ACCESS_TOKEN的值过一段时间会失效,基本上所有的读写API的访问都需要这个值,所以需要对ACCESS_TOKEN的值进行验证,判断这个值是否失效,在MIS系统打开微博界面的时候首先用原先的ACCESS_TOKEN值进行访问,如果不能访问,则跳转到重新授权页面,在授权页面进行授权之后,将ACCESS_TOKEN值写入数据库配置项中,这样对接接口的代码也可以获取到最新的值去访问API接口了,如果原先的ACCESS_TOKEN值可以使用,则进入MIS中微博页面。

5.对多媒体的处理

由于现场的服务器对IP进行了限制,新浪那边存的图片地址都是不一样的,这样不能将图片直接进行写入过来,这边的处理方式是将图片地址保存到数据库中,然后在前端页面中解析之后,通过客户电脑直接访问那个图片链接,而不是访问部署项目的服务器那边。

6.部分代码(其他对接API接口代码可参照)

//获取@我的微博内容

String jsonStr =

readContentFromGet(url3);

try{

JSONObject obj =

newJSONObject(jsonStr.toString());

JSONArray obj1 =

obj.getJSONArray("statuses");

if(obj1!=null){

for(inti

= obj1.length()-1; i >=0; i--) {

JSONObject temp =

newJSONObject(obj1.getString(i));

try{

saveWeiBo(8,

temp.getString("id"),

"2015-12-07 19:28:29", "旧数据录入",

"123", "123","media");

JSONObject extInfo =

temp.getJSONObject("retweeted_status");

String id =

extInfo.getString("id");

Date d =

newDate(extInfo.getString("created_at"));

String time =

newSimpleDateFormat("yyyy-MM-dd

HH:mm:ss").format(d);

String text =

extInfo.getString("text");

//获取用户信息

JSONObject userInfo =

extInfo.getJSONObject("user");

String

userID=userInfo.getString("id");

String name =

userInfo.getString("name");

//获取图片

JSONArray obj2 =

extInfo.getJSONArray("pic_urls");

String

mediaURLs="";

if((obj2==null)||(obj2.length()==0)){

mediaURLs="media";

}else{

for(intj

= obj2.length()-1; j>=0; j--) {

JSONObject temp1 =

newJSONObject(obj2.getString(j));

if(mediaURLs.length()==0){

mediaURLs=temp1.getString("thumbnail_pic");

}else{

mediaURLs=mediaURLs+","+temp1.getString("thumbnail_pic");

}

}

}

saveWeiBo(2,id,time,text,userID,name,mediaURLs);

//System.out.println(extInfo.toString());

//System.out.println("id:"+id+"

time:"+time+"

text"+text+"

userID:"+userID+"

name:"+name);

}catch(JSONException

e) {

}

}

}

} catch(Exception

e) {

logger.error("获取json异常:"+e.getMessage());

e.printStackTrace();

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值