如何获取新浪微博的access token-CSDN论坛

 我来答
一骑当后
推荐于2016-09-24 · 知道合伙人数码行家
一骑当后
知道合伙人数码行家
采纳数:40298 获赞数:306440
网络、设备维护、电路、弱电检测。

向TA提问 私信TA
展开全部
希望能帮到你:
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(),"测试呢");
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式