新浪微博自动获取access_token问题

kkyxzy 2016-02-18 09:59:27

package weibo4j.examples.timeline;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;
import weibo4j.util.WeiboConfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class sinaWeiBoUL {
/***
* 模拟登录并得到登录后的Token
* @param username 用户名
* @param password 密码
* @return
* @throws HttpException
* @throws IOException
*/
public static AccessToken getToken(String username,String password) throws HttpException, IOException{
String clientId = WeiboConfig.getValue("client_ID") ;
String redirectURI = WeiboConfig.getValue("redirect_URI") ;
String url = WeiboConfig.getValue("authorizeURL");

PostMethod postMethod = new PostMethod(url);
//应用的App Key
postMethod.addParameter("client_id",clientId);
//应用的重定向页面
postMethod.addParameter("redirect_uri",redirectURI);
//模拟登录参数
//开发者或测试账号的用户名和密码
postMethod.addParameter("userId", username);
postMethod.addParameter("passwd", password);
postMethod.addParameter("isLoginSina", "0");
postMethod.addParameter("action", "submit");
postMethod.addParameter("response_type","code");
HttpMethodParams param = postMethod.getParams();
param.setContentCharset("UTF-8");
//添加头信息
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Referer", "https://api.weibo.com/oauth2/authorize?client_id="+clientId+"&redirect_uri="+redirectURI+"&from=sina&response_type=code"));
headers.add(new Header("Host", "api.weibo.com"));
headers.add(new Header("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"));
HttpClient client = new HttpClient();
client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
client.executeMethod(postMethod);
int status = postMethod.getStatusCode();
System.out.println(status);
if (status != 302)
{
System.out.println("token刷新失败");
return null;
}
//解析Token
Header location = postMethod.getResponseHeader("Location");
if (location != null)
{
String retUrl = location.getValue();
int begin = retUrl.indexOf("code=");
if (begin != -1) {
int end = retUrl.indexOf("&", begin);
if (end == -1)
end = retUrl.length();
String code = retUrl.substring(begin + 5, end);
if (code != null) {
Oauth oauth = new Oauth();
try{
AccessToken token = oauth.getAccessTokenByCode(code);
return token;
}catch(Exception e){
e.printStackTrace();
}
}
}
}
return null;
}
/**
* 发微博
* @param token 认证Token
* @param content 微博内容
* @return
* @throws Exception
*/
public static boolean sinaSendWeibo(String token,String content) throws Exception {
boolean flag = false ;
Timeline timeline = new Timeline();
timeline.client.setToken(token);
try
{
timeline.UpdateStatus(content);
flag = true ;
}
catch (WeiboException e)
{
flag = false ;
System.out.println(e.getErrorCode());
}
return flag;
}


public static void main(String[] args) throws Exception
{
AccessToken at = getToken("用户","密码");
System.out.println(at.getAccessToken());
sinaSendWeibo(at.getAccessToken(),"测试呢");
}
}

使用该代码报
200
token刷新失败
Exception in thread "main" java.lang.NullPointerException
at weibo4j.examples.Sina.main(Sina.java:114)求问如何解决
...全文
517 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiping8000 2018-06-12
  • 打赏
  • 举报
回复
两年前的问题了 不知道今年解决没有 急求
Sydnie小猫猫 2016-12-27
  • 打赏
  • 举报
回复
引用 3 楼 a276202031 的回复:
竟然挖坟
怎样?有没有解决?
浮云若水 2016-12-27
  • 打赏
  • 举报
回复
竟然挖坟
Sydnie小猫猫 2016-12-27
  • 打赏
  • 举报
回复
我也遇到了相同的问题,楼主解决了吗?
kkyxzy 2016-02-18
  • 打赏
  • 举报
回复
没人回答吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧