当前位置: 移动技术网 > IT编程>开发语言>Java > 编写调用新浪微博API的Java程序来发送微博

编写调用新浪微博API的Java程序来发送微博

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

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

20151126170347058.jpg (269×354)

接下来,到这个网址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。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网