java 实现微博推送_编写调用新浪微博API的Java程序来发送微博

本文介绍了如何利用Java SDK和新浪API创建一个程序来自动发送微博。首先下载微博SDK,然后在MyEclipse中新建项目并导入SDK。接着注册应用获取app key和app secret,配置config.properties文件。通过OAuth2流程获取access_token,最后使用这个token更新微博状态,实现微博自动发布。遇到权限问题时,可以在应用信息中添加测试账号来解决问题。
摘要由CSDN通过智能技术生成

首先,需要下载新浪微博的sdk,这里附上地址:http://vdisk.weibo.com/s/z7ifc2gccwc1b

下载完了之后解压,然后打开myeclipse,新建项目,再把刚才解压出来的import到项目中。如图所示:

580d4188bd0fde56fdf7e355031d1475.png

接下来,到这个网址http://open.weibo.com/注册应用。有三种应用,选择站内应用,然后创建应用。把该填写的都填写上。确认就ok。需要注意的是有两点:

1,是注册完应用,会有app key以及app secret,这个接下来会用到。

2,到《我的应用》,选择:应用信息,

点击“编辑”,然后注意到有两个地址:其中“应用实际地址”要慎重填写,我的是http://127.0.0.1/callback.jsp,注意地址后面不要有“/”这些。现在,记住这个地址,接下来要用到。

现在,转向myeclipse,src下,有config.properties文件,编辑它

client_id就是app key。

client_sercret就是app secret

redirect_uri就是之前着重提到的那个地址:http://127.0.0.1/callback.jsp

保存。

接下来,我们发送一条微博:

examples下,weibo4j.examples.oauth2包下:oauth4code类,直接运行:会出现应用授权页面,输入用户名和密码之后,就会跳到之前填写的那个redirect_uri页面。如果浏览器中保存着微博的信息,则不经过这个页面而直接跳转到redirect_uri页面。

在跳转到redirect_uri页面后,看到url地址栏里,格式是redirect_uri?code=xxxxxx。下面,复制code的值,然后进入到myeclipse的控制台输出那里,看到:

code=https://api.weibo.com/oauth2/authorize?client_id=1458508643&redirect_uri=http://127.0.0.1/callback.jsp&response_type=code

hit enter when it's done.[enter]:

接下来,粘帖code到[enter]:后面。回车。就会看到一大堆输出信息。

直接跳到最后,看到access_token,这个就是我们需要的东西了,记录下来。

接下来发送微博:

在weibo4j.examples.timeline包下,updatestatus类下/

需要传递两个参数进去,我就直接写了进去。代码如下:

packageweibo4j.examples.timeline;

importweibo4j.timeline;

importweibo4j.weibo;

importweibo4j.examples.oauth2.log;

importweibo4j.model.status;

importweibo4j.model.weiboexception;

publicclassupdatestatus {

publicstaticvoidmain(string[] args) {

string access_token = "2.00lbva1cxikhabfbc0d2a0c10fwtti";

string statuses = "此条微博来自星光发布系统发布";

weibo weibo = newweibo();

weibo.settoken(access_token);

timeline tm = newtimeline();

try{

status status = tm.updatestatus(statuses);

log.loginfo(status.tostring());

} catch(weiboexception e) {

e.printstacktrace();

} }

}

package weibo4j.examples.timeline;

import weibo4j.timeline;

import weibo4j.weibo;

import weibo4j.examples.oauth2.log;

import weibo4j.model.status;

import weibo4j.model.weiboexception;

public class updatestatus {

public static void main(string[] args) {

string access_token = "2.00lbva1cxikhabfbc0d2a0c10fwtti";

string statuses = "此条微博来自星光发布系统发布";

weibo weibo = new weibo();

weibo.settoken(access_token);

timeline tm = new timeline();

try {

status status = tm.updatestatus(statuses);

log.loginfo(status.tostring());

} catch (weiboexception e) {

e.printstacktrace();

} }

}

statuses就是想要发表的东西。

到此,就可以发送微博了。

有问题留言。!

附:

有时候,比如用另外一个微博帐号(就是区别于注册应用的这个帐号)时,会出现一些错误。错误信息:

{"error":"applications over the unaudited use restrictions!","error_code":21321,"request":"/2/statuses/update.json"}

eibo4j.model.weiboexception: 403:the request is understood, but it has been refused. an accompanying error message will explain why.

error:applications over the unaudited use restrictions! error_code:21321/2/statuses/update.json

at weibo4j.http.httpclient.httprequest(httpclient.java:414)

at weibo4j.http.httpclient.httprequest(httpclient.java:372)

at weibo4j.http.httpclient.post(httpclient.java:301)

at weibo4j.http.httpclient.post(httpclient.java:286)

at weibo4j.timeline.updatestatus(timeline.java:708)

at weibo4j.examples.timeline.updatestatus.main(updatestatus.java:18)

{"error":"applications over the unaudited use restrictions!","error_code":21321,"request":"/2/statuses/update.json"}

weibo4j.model.weiboexception: 403:the request is understood, but it has been refused. an accompanying error message will explain why.

error:applications over the unaudited use restrictions! error_code:21321/2/statuses/update.json

at weibo4j.http.httpclient.httprequest(httpclient.java:414)

at weibo4j.http.httpclient.httprequest(httpclient.java:372)

at weibo4j.http.httpclient.post(httpclient.java:301)

at weibo4j.http.httpclient.post(httpclient.java:286)

at weibo4j.timeline.updatestatus(timeline.java:708)

at weibo4j.examples.timeline.updatestatus.main(updatestatus.java:18)

这个的解决办法:还是在我的应用那里,点击”应用信息“,”测试账号“,看到有添加测试账号,这个时候,你把想要发布微博的帐号的用户昵称添加进去,就ok。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值