日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 实现微博,QQ联合登录

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 实现微博,QQ联合登录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

詳見:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt313

開發平臺?
http://connect.qq.com/??
http://open.weibo.com/?
可以下載相應的sdk 和 API?

Java代碼

  • ?

  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 package?com.fanbaobao.action;?? ??? import?javax.servlet.http.HttpServletRequest;?? import?javax.servlet.http.HttpServletResponse;?? import?javax.servlet.http.HttpSession;?? ??? import?com.fanbaobao.service.FbbUser;?? import?com.fanbaobao.service.FbbService.Client;?? import?com.fanbaobao.util.FbbUtil;?? ??? import?weibo4j.Oauth;?? import?weibo4j.Users;?? import?weibo4j.Weibo;?? import?weibo4j.http.AccessToken;?? import?weibo4j.model.User;?? public?class?SinaAction?extends?BaseAction?{?? ????private?String?errorMsg;??? ????public?String?toLogin()throws?Exception{?? ????????try?{?? ????????????HttpServletRequest?request=getRequest();?? ????????????Oauth?oauth=new?Oauth();?? ????????????getResponse().sendRedirect(oauth.authorize("code"));?//重定向到新浪授權頁面?? ????????}?catch?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????????return?null;?? ????}?? ??????? ????/**? ?????*?新浪微博登錄的回調? ?????*?@return? ?????*?@throws?Exception? ?????*/?? ????public?String?doLogin()?throws?Exception{?? ??????????? ????????HttpServletRequest?request=getRequest();?? ????????HttpSession?session=request.getSession();?? ????????String?accesstoken???=?null;?? ????????String?code=request.getParameter("code");?? ????????if(code!=null)?? ????????{?? ??????????? ????????????Oauth?oauth=new?Oauth();?? ????????????AccessToken?accessToken=oauth.getAccessTokenByCode(code);?? ????????????accesstoken=accessToken.getAccessToken();?? ????????????if(accessToken!=null)?? ????????????{?? ????????????????Weibo?weibo=new?Weibo();?? ????????????????weibo.setToken(accessToken.getAccessToken());?? ????????????????Users?users=new?Users();?? ????????????????User?weiboUser=users.showUserById(accessToken.getUid());?? ????????????????HttpServletResponse?response=getResponse();?? ????????????????System.out.println(weiboUser.getId());?? ????????????????System.out.println(weiboUser.getGender());?? ????????????????System.out.println(weiboUser.getName());?? ????????????????System.out.println(accesstoken);?? //??????????????if(first<2){?? //??????????????????if(first==1){?//用戶第一次登錄,發一條微博,且follow?51bi?? //??????????????????????Timeline?timeline=new?Timeline();?? //??????????????????????timeline.UpdateStatus("我注冊成為了#比購網#會員,免費注冊后去淘寶購物,最高有35%的現金返還。最給力的是:到拉手、京東商城、1號店等400多家知名商城不僅能享受最低價格,還能額外省錢40%。喜歡網購的童鞋都去注冊一個省錢吧!官方網址:http://www.51bi.com/space/biuser/register.jsp?currentUrl=http://www.51bi.com/taobao/");?? //??????????????????????try?{?? //??????????????????????????Friendships?fm?=?new?Friendships();?? //??????????????????????????fm.createFriendshipsById("1718952754");//follow?51bi?? //??????????????????????????fm.createFriendshipsById("2908640880");//follow?51bi?? //??????????????????????}?catch?(Exception?e)?{?? //??????????????????????}?? //??????????????????}?? ????????????????????//未完善資料?? ????????????????????Client?client?=?FbbUtil.getClient();?? ????????????????????FbbUser?fbbUser=?client.doOauth(1,?weiboUser.getId());?? ????????????????????session.setAttribute("fbbUserInfo",?fbbUser);????? ????????????????????return?SUCCESS;?? ????????????????}?? ??????? ????????????}else{?? ????????????????errorMsg="新浪oauth?認證請求非法!";?? ????????????????return?LOGIN;?? ????????????}?? ????????return?SUCCESS;?? ????}?? ??????? ??????? ??????? ??????? ????public?String?getErrorMsg()?{?? ????????return?errorMsg;?? ????}?? ??? ????public?void?setErrorMsg(String?errorMsg)?{?? ????????this.errorMsg?=?errorMsg;?? ????}?? }
  • ?

    Java代碼

  • ?

  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 package?com.fanbaobao.action;?? ??? import?javax.servlet.http.HttpServletRequest;?? import?javax.servlet.http.HttpServletResponse;?? import?javax.servlet.http.HttpSession;?? ??? import?com.fanbaobao.service.FbbUser;?? import?com.fanbaobao.service.FbbService.Client;?? import?com.fanbaobao.util.FbbUtil;?? import?com.qq.connect.QQConnectException;?? import?com.qq.connect.api.OpenID;?? import?com.qq.connect.api.qzone.PageFans;?? import?com.qq.connect.api.qzone.UserInfo;?? import?com.qq.connect.javabeans.AccessToken;?? import?com.qq.connect.javabeans.qzone.PageFansBean;?? import?com.qq.connect.javabeans.qzone.UserInfoBean;?? import?com.qq.connect.oauth.Oauth;?? ??? public?class?QzoneAction?extends?BaseAction?{?? private?String?errorMsg;??? ??? ????public?String?toLogin()throws?Exception{?? ????????this.getResponse().setContentType("text/html;charset=utf-8");?? ????????try?{?? ????????????this.getResponse().sendRedirect(new?Oauth().getAuthorizeURL(this.getRequest()));?? ????????}?catch?(QQConnectException?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????????return?null;?? ????}?? ??????? ????public?String?doLogin()throws?Exception{?? ?????????HttpServletResponse?response=this.getResponse();?? ?????????HttpServletRequest?request=this.getRequest();?? ?????????HttpSession?session=request.getSession();?? ?????????response.setContentType("text/html;?charset=utf-8");?? ????????????try?{?? ????????????????AccessToken?accessTokenObj?=? ????????????????(new?Oauth()).getAccessTokenByRequest(request);?? ??????????????????? ????????????????String?accessToken???=?null,?? ???????????????????????openID????????=?null;?? ????????????????Long?tokenExpireIn?=?null;?? ??? ??? ????????????????if?(accessTokenObj.getAccessToken().equals(""))?{?? ????????????????????errorMsg="QQ登錄校驗失敗!";?? ????????????????????return?LOGIN;?? ????????????????}?else?{?? ????????????????????accessToken?=?accessTokenObj.getAccessToken();?? ????????????????????tokenExpireIn?=?accessTokenObj.getExpireIn();?? ??????????????????????? ????????????????????request.getSession().setAttribute("demo_access_token",?accessToken);?? ????????????????????request.getSession().setAttribute( ????????????????????"demo_token_expirein",?String.valueOf(tokenExpireIn));?? ????????????????????OpenID?openIDObj?=??new?OpenID(accessToken);?? ????????????????????openID?=?openIDObj.getUserOpenID();?? ??? ????????????????????request.getSession().setAttribute("demo_openid",?openID);?? ????????????????????UserInfo?qzoneUserInfo?=?new?UserInfo(accessToken,?openID);?? ????????????????????UserInfoBean?userInfoBean?=?qzoneUserInfo.getUserInfo();?? //?? ????????????????????PageFans?pageFansObj?=?new?PageFans(accessToken,?openID);?? ????????????????????PageFansBean?pageFansBean?=?pageFansObj.checkPageFans("97700000");?? ????????????????????com.qq.connect.api.weibo.UserInfo?weiboUserInfo?=? ????????????????????new?com.qq.connect.api.weibo.UserInfo(accessToken,?openID);?? ????????????????????com.qq.connect.javabeans.weibo.UserInfoBean?weiboUserInfoBean?= ?????????????????????weiboUserInfo.getUserInfo();?? ??? ?????????????????//?第三方處理用戶綁定邏輯?? ????????????????????String?user_id=openID;?? ????????????????????String?nickname=new?String(userInfoBean.getNickname());?? ????????????????????System.out.println(user_id);?? ????????????????????System.out.println(nickname);?? ????????????????????Client?client?=?FbbUtil.getClient();?? ????????????????????FbbUser?fbbUser=?client.doOauth(2,?user_id);?? ????????????????????session.setAttribute("fbbUserInfo",?fbbUser);????? ????????????????????return?SUCCESS;?? //??????????????????session.setAttribute("oauth_token",?oauth_token);?? //??????????????????session.setAttribute("oauth_token_secret",?oauth_token_secret);?? //??????????????????InterUser?interUser=new?InterUser( ????????????????????????????????????????????????????????????Constants.LOGIN_FROM_QQ,user_id,null,nickname);?? //??????????????????interUser.setBiUserService(biUserService);?? //??????????????????interUser.setRuledao(ruledao);?? //??????????????????interUser.setAccesstoken(accessToken);?? //??????????????????if?(tokenExpireIn!=null)?{?? //??????????????????????interUser.setExpireIn(String.valueOf(tokenExpireIn));?? //??????????????????}?? //??????????????????session.setAttribute("uid",?user_id);?? //??????????????????session.setAttribute("fromsite",?Constants.LOGIN_FROM_QQ);?? //??????????????????int?first=interUser.doUserLogin(request,response,false);?//接口登錄?? //??????????????????if(first<2){?? //??????????????????????session.setAttribute("loginname",nickname);?? //??????????????????????if?(first==-1)?{?? //??????????????????????????session.setAttribute("msg","save?userBind?error");?? //??????????????????????}else?if?(first==-2)?{?? //??????????????????????????session.setAttribute("msg","error");?? //??????????????????????}?? //??????????????????????return?"qzone";?? //??????????????????}?????????? ??? ????????????????}?? ????????????}?catch?(QQConnectException?e)?{?? ????????????????e.printStackTrace();?? ????????????????return?LOGIN;?? ????????????}?? ????}?? ??????? ??????? ??????? ????public?String?getErrorMsg()?{?? ????????return?errorMsg;?? ????}?? ??? ????public?void?setErrorMsg(String?errorMsg)?{?? ????????this.errorMsg?=?errorMsg;?? ????}?? }
  • ?

    Java代碼

  • //login.properties??

  • ############??

  • #t.sina?login??

  • ###########??

  • client_ID?=xx?????

  • client_SERCRET?=xx??

  • redirect_URI?=xxx??

  • baseURL=https://api.weibo.com/2/??

  • accessTokenURL=https://api.weibo.com/oauth2/access_token??

  • authorizeURL=https://api.weibo.com/oauth2/authorize??

  • ############??

  • #taoBao?login??

  • ###########??

  • ??

  • #TAOBAO_KEY=xxxx??

  • #TAOBAO_SECRET=xxxxxx??

  • #TAOBAO_REDIRECT=xxxx??

  • #authorizeURL=https://oauth.taobao.com/authorize??

  • #accessTokenURL=https://oauth.taobao.com/token??

  • ??

  • //qqconnectconfig.properties??

  • app_ID?=?x??

  • app_KEY?=?xx??

  • redirect_URI?=?xxx??

  • scope?=?get_user_info,add_topic,add_one_blog,add_album,upload_pic,list_album,add_share,check_page_fans,add_t,

  • add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_ido,

  • get_tenpay_addr??

  • baseURL?=?https://graph.qq.com/??

  • getUserInfoURL?=?https://graph.qq.com/user/get_user_info??

  • accessTokenURL?=?https://graph.qq.com/oauth2.0/token??

  • authorizeURL?=?https://graph.qq.com/oauth2.0/authorize??

  • getOpenIDURL?=?https://graph.qq.com/oauth2.0/me??

  • addTopicURL?=?https://graph.qq.com/shuoshuo/add_topic??

  • addBlogURL?=?https://graph.qq.com/blog/add_one_blog??

  • addAlbumURL?=?https://graph.qq.com/photo/add_album??

  • uploadPicURL?=?https://graph.qq.com/photo/upload_pic??

  • listAlbumURL?=?https://graph.qq.com/photo/list_album??

  • addShareURL?=?https://graph.qq.com/share/add_share??

  • checkPageFansURL?=?https://graph.qq.com/user/check_page_fans??

  • addTURL?=?https://graph.qq.com/t/add_t??

  • addPicTURL?=?https://graph.qq.com/t/add_pic_t??

  • delTURL?=?https://graph.qq.com/t/del_t??

  • getWeiboUserInfoURL?=?https://graph.qq.com/user/get_info??

  • getWeiboOtherUserInfoURL?=?https://graph.qq.com/user/get_other_info??

  • getFansListURL?=?https://graph.qq.com/relation/get_fanslist??

  • getIdolsListURL?=?https://graph.qq.com/relation/get_idollist??

  • addIdolURL?=?https://graph.qq.com/relation/add_idol??

  • delIdolURL?=?https://graph.qq.com/relation/del_idol??

  • getTenpayAddrURL?=?https://graph.qq.com/cft_info/get_tenpay_addr??

  • getRepostListURL?=?https://graph.qq.com/t/get_repost_list??

  • version?=?2.0.0.0??

  • 轉載于:https://www.cnblogs.com/grefr/p/6089075.html

    總結

    以上是生活随笔為你收集整理的java 实现微博,QQ联合登录的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。