新浪微博OAuth2.0使用Java示例

jspet 9年前發布 | 124K 次閱讀 OAuth2.0 OAuth開發包

1.首先熟悉OAuth認證流程;熟悉app-key,app-secret,request-token,access-token幾個概念:

    a.在oauth認證中有三個參與者:user,service provider和consumer;其中service provider為新浪微博平臺,consumer為新浪應用程序開發者,user為應用程序的使用用戶。

    b.user和service provider之間是信任的,consumer和service provider之間是信任的;但user與consumer之間不是信任的。OAuth認證的核心是實現user和consumer之間的信任。

   c.首先consumer到新浪微博站內應用開發中申請App-key和app-secret,完成consumer和service provider之間的信任;然后consumer根據appKey和callback URl生成request token帶著user去新浪的登錄認證頁面;

用戶認證后,生成access-token返回給consumer使用;consumer根據此access-token替用戶完成某些操作或任務。

2.例子

配置信息:config.properties中:client_ID和client_SECRET填寫申請到的appkey和appsecret兩個選項。

redirect_url:填寫自己的回調地址。(本例為:http://localhost/weiboTest/callbackURL.jsp)。同時在微博站內應用:應用信息-》基本信息-》應用實際地址上填寫該回調地址.

新建web project:weiboTest

a.核心函數.

public class WeiboAuth {
 public void getRequestToken() {
  Oauth oauth = new Oauth();

  BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
 }

 public AccessToken getAccessToken(String code) {
  Oauth oauth = new Oauth();
  AccessToken accessToken = null;
   accessToken = oauth.getAccessTokenByCode(code);
  return accessToken;
 }

 public void updateStatus(String accessToken, String content) {
  Timeline tl = new Timeline();
  tl.client.setToken(accessToken);
   Status status = tl.UpdateStatus(content);
 }

}


b.request.jsp //站點首頁

 <html>
 <head>
   <base href="<%=basePath%>">
   <title>起始頁面</title> 
 </head>
 <body>
 <jsp:useBean id="weboauth" scope="session" class="com.geron.test.WeiboAuth"/>
 <% weboauth.getRequestToken(); %>
  <a href="index.jsp?opt=1">請點擊進行web方式的OAuth認證!!!</a>
 </body>
</html>

c.callbackURL.jsp //回調頁面,成功后跳轉到相應的頁面

<html>
 <head>
   <base href="<%=basePath%>">  
 </head>
  <body>
 <jsp:useBean id="weiboAuth" scope="session" class="com.geron.test.WeiboAuth" />
 <%
  String code=request.getParameter("code");
  AccessToken accessToken=weiboAuth.getAccessToken(code);
  if(accessToken!=null){
   session.setAttribute("accessToken",accessToken.getAccessToken().toString());
   out.print("5秒鐘后跳轉...");
   Thread.sleep(5000);
   response.sendRedirect(basePath+"/writeweibo.jsp");
  }else{
   response.sendRedirect(basePath+"/index.jsp");
  }
 %>
 </body>
</html>

d. writeweibo.jsp//發微博頁面

<html>
 <head>
  <base href="<%=basePath%>">
  <title>寫微博</title>
 </head>
 <body bgcolor="#d0d0d0">
  <form method="post" action="<%=basePath%>/servlet/updateWeibo">
   請在這里寫上140字符以內的文本:</br>
   <textarea name="weiboText" rows="6" cols="30">發微薄</textarea></br>
   <input type="submit" value="發布">
   <input type="reset" value="清除"></br>
  </form>
 </body>
</html>

e.updateweibo.java//servlet函數

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  String weiboText = new String(request.getParameter("weiboText").getBytes("iso-8859-1"),"utf-8");
  WeiboAuth auth = new WeiboAuth();
  HttpSession session = request.getSession();
  String accessToken = (String) session.getAttribute("accessToken");
  System.out.println("accessToken is : "+session.getAttribute("accessToken"));
  auth.updateStatus(accessToken, weiboText);
  out.print("發布成功");
  response.sendRedirect("/index.jsp");
 }

 本文由用戶 jspet 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!