- A:消费方请求Request Token
- B:服务提供者授权Request Token
- C:消费方定向用户到服务提供者
- D:获得用户授权后,服务提供者定向用户到消费方
- E:消费方请求Access Token
- F:服务提供者授权Access Token
- G:消费方访问受保护的资源
首先是要在网易开发平台申请消费者开发的key和secret字符串
call.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page language="java" import="t4j.*" %> <%@ page language="java" import="t4j.http.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title></title> </head> <body> <% System.setProperty("tblog4j.oauth.consumerKey", "*****") ; System.setProperty("tblog4j.oauth.consumerSecret", "************") ; TBlog tblog = new TBlog(); try { RequestToken requestToken = tblog.getOAuthRequestToken() ; session.setAttribute("requestToken",requestToken) ; String url = "http://api.t.163.com/oauth/authenticate?oauth_token=" + requestToken.getToken() + "&oauth_token_secret=" + requestToken.getTokenSecret() + "&oauth_callback=http://localhost:8080/weibo_163/callback.jsp" ; response.sendRedirect(url) ; } catch (TBlogException e1) { e1.printStackTrace() ; } %> </body> </html>
callback.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page language="java" import="t4j.*" %> <%@ page language="java" import="t4j.http.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% RequestToken requestToken = (RequestToken)session.getAttribute("requestToken"); TBlog tblog = new TBlog(); AccessToken token = tblog.getOAuthAccessToken(requestToken); tblog.setToken(token.getToken(),token.getTokenSecret()); try { out.println(tblog.updateStatus("update status from Java SDK_163").getId()); } catch (TBlogException e) { e.printStackTrace(); } %> </body> </html>
运行时,会出现下图-授权:
输入你的邮箱 密码 进行授权,之后,返回callback.jsp页面,jsp页面中的方法updateStatus(String)成功执行后,在你的微博里会成功发表一篇微博。