protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String urlNameString = "https://api.weibo.com/oauth2/access_token";
String code =request.getParameter("code");
//可以使用部分用户权限的token,可以使网站代替用户操作的关键认证
AccessToken accessToken=null;
// 根据地址获取请求
HttpPost request2 = new HttpPost(urlNameString);//这里发送get请求
// 封装参数
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("client_id", "你的client_id"));
params.add(new BasicNameValuePair("client_secret", "你的clien_secret"));
params.add(new BasicNameValuePair("redirect_uri", "http://TestDemoYR/reverse.action"));
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
params.add(new BasicNameValuePair("code","CODE"));
request2.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//发送请求获取响应,这里response主要的作用是获取token
HttpResponse response2=new DefaultHttpClient().execute(request2);
//通过oauth获取AccessToken
Oauth oauth = new Oauth();
accessToken =oauth.getAccessTokenByCode(code);
//此处就可以通过token发送文章了!
Timeline timeline = new Timeline("2.00d3BzZFbEVFoCf372edcac6NulR9E");
Status status = timeline.updateStatus("用程序发微博");
Log.logInfo(status.toString());
} catch (WeiboException e1) {
e1.printStackTrace();
}
}
新浪微博网站接入开发(渣浪的api写的骗人,不用urlencoding!!!!!!)
最新推荐文章于 2021-01-07 15:59:41 发布