/** * 获取第三方用户信息 * * @param siteMark * @param openId * @param accessToken * @return */ public static BindSite getBindSite(SiteMark siteMark, String openId, String accessToken) { BindSite bs = new BindSite(); bs.setSiteMark(siteMark); bs.setOpenId(openId); bs.setAccessToken(accessToken); String nickName = null; if (SiteMark.QQ.equals(siteMark)) { UserInfo ui = new UserInfo(accessToken, openId); UserInfoBean uib = null; try { uib = ui.getUserInfo(); } catch (QQConnectException e) { logger.error(e); throw new ServiceException(ErrService.UserS.ACC_108, "QQ_API调用异常", "第三方平台异常,请从新尝试!"); } nickName = uib.getNickname(); } else if (SiteMark.SINA.equals(siteMark)) { Users um = new Users(); um.client.setToken(accessToken); try { weibo4j.model.User user = um.showUserById(openId); nickName = user.getScreenName(); } catch (WeiboException e) { logger.error(e); throw new ServiceException(ErrService.UserS.ACC_108, "SINA_API调用异常", "第三方平台异常,请从新尝试!"); } } bs.setNickName(nickName); return bs; }
@Override protected String getNickName(Map<String, Object> oParaMap) { AccessToken accessToken = (AccessToken) oParaMap.get("token"); weibo.setToken(accessToken.getAccessToken()); try { User ur = um.showUserById(accessToken.getUid()); return ur.getScreenName(); } catch (WeiboException e) { log.error("获取新浪昵称失败", e); } return null; }
public static void main(String[] args) { String access_token = args[0]; String id = args[1]; Friendships fm = new Friendships(access_token); try { UserWapper users = fm.getFriendsByID(id); for (User u : users.getUsers()) { System.out.println(u.toString()); } System.out.println(users.getNextCursor()); System.out.println(users.getPreviousCursor()); System.out.println(users.getTotalNumber()); } catch (WeiboException e) { e.printStackTrace(); } }
@Override public boolean GetAndSaveSinaFriends(User user, String token) { // TODO Auto-generated method stub Friendships friendships = new Friendships(); friendships.setToken(token); int cursor = 0; String[] friendIds = null; try { friendIds = friendships.getFriendsIdsByUid(user.getId(), 5000, cursor); } catch (WeiboException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (friendIds == null) return false; return scholarUserDao.addSinaFriends(user, friendIds); }
@RequestMapping("/bindWeibo") public void weiboLogin(HttpServletRequest request, HttpServletResponse response, String code) { try { ThirdPartyAccess xinlang = thirdPartyAccessService.findByType(ThirdPartyAccess.TYPE_XINLANG); GlobalSetting globalSetting = (GlobalSetting) request.getSession().getAttribute("setting"); weibo4j.Oauth oauth = new weibo4j.Oauth(); weibo4j.http.AccessToken accessTokenObj = oauth.getAccessTokenByCode( code, xinlang.getAccessKey(), xinlang.getAccessSecret(), "http://" + globalSetting.getAppUrl() + "/profile/bindWeibo"); String accessToken = null, uid = null, tokenExpireIn = null; if ("".equals(accessTokenObj.getAccessToken())) { // 我们的网站被CSRF攻击了或者用户取消了授权 // 做一些数据统计工作 LOG.info("没有获取到响应参数"); } else { accessToken = accessTokenObj.getAccessToken(); tokenExpireIn = accessTokenObj.getExpireIn(); request.getSession().setAttribute("token_expirein", String.valueOf(tokenExpireIn)); // 利用获取到的accessToken 去获取当前用的uid -------- start Account am = new Account(accessToken); JSONObject uidObj = am.getUid(); uid = uidObj.getString("uid"); request.getSession().setAttribute("openId", uid); request.getSession().setAttribute("loginType", UserSession.TYPE_XINLANG); // 利用获取到的accessToken 去获取当前用户的openid --------- end // 为空代表首次登录,此处获取的信息尚未完全 ThirdPartyAccount tpa = tpaService.findByOpenId(uid); if (tpa == null) { // 获取新浪微博用户的信息 Users um = new Users(accessToken); weibo4j.model.User wUser = um.showUserById(uid); tpa = new ThirdPartyAccount(); tpa.setOpenId(uid); tpa.setAccountType(UserSession.TYPE_XINLANG); tpa.setAccessToken(accessToken); tpa.setHeadIconHD(wUser.getAvatarHD()); tpa.setHeadIconBig(wUser.getAvatarLarge()); tpa.setHeadIconMid(wUser.getProfileImageUrl()); tpa.setHeadIconSmall(wUser.getProfileImageUrl()); tpa = tpaService.update(tpa); } UserSession userSession = (UserSession) request.getSession(false).getAttribute("userSession"); tpa.setUser(userSession.getUser()); tpaService.update(tpa); response.sendRedirect("thirdParty"); } } catch (IOException e) { LOG.error("重定向回本站失败", e); } catch (WeiboException e) { LOG.error("连接到新浪失败", e); } catch (JSONException e) { LOG.error("JSON解析错误", e); } }