示例#1
0
  @SuppressWarnings("unchecked")
  protected Profile authLogin() throws Exception {
    String presp;
    try {
      Response response = authenticationStrategy.executeFeed(PROFILE_URL);
      presp =
          response.getResponseBodyAsString(
              Constants.ENCODING); // callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
      if (presp != null) {
        presp = presp.trim().intern();
        if (presp.startsWith("callback(") && presp.endsWith(");")) {
          presp = presp.substring(presp.indexOf("{"), presp.indexOf("}") + 1);
          Map<String, String> map = (Map<String, String>) Json.fromJson(presp);
          if (map.get("openid") != null) {
            Profile p = new Profile();
            p.setValidatedId(map.get("openid")); // QQ定义的
            p.setProviderId(getProviderId());
            userProfile = p;

            try {
              Map<String, String> params = new HashMap<String, String>();
              params.put("format", "json");
              params.put("openid", map.get("openid"));
              params.put("oauth_consumer_key", config.get_consumerKey());
              response =
                  authenticationStrategy.executeFeed(
                      "https://graph.qq.com/user/get_user_info", "GET", params, null, null);
              presp = response.getResponseBodyAsString(Constants.ENCODING);
              Map<String, Object> user_info = (Map<String, Object>) Json.fromJson(presp);
              if ((Integer) user_info.get("ret") == 0) { // 获取成功
                if (user_info.get("nickname") != null)
                  p.setDisplayName(user_info.get("nickname").toString());
                if (user_info.get("figureurl") != null)
                  p.setProfileImageURL(user_info.get("figureurl").toString());
                if (user_info.get("gender") != null)
                  p.setGender(user_info.get("gender").toString());
              }

              // TODO 尝试获取Email等详细信息

            } catch (Throwable e) {
              e.printStackTrace();
            }

            return p;
          }
        }
      }

      throw new SocialAuthException("QQ Error : " + presp);
    } catch (Exception e) {
      throw new SocialAuthException("Error while getting profile from " + PROFILE_URL, e);
    }
  }
示例#2
0
  @SuppressWarnings("unchecked")
  protected Profile authLogin() throws Exception {
    String presp;

    try {
      Response response = authenticationStrategy.executeFeed(PROFILE_URL);
      presp = response.getResponseBodyAsString(Constants.ENCODING);
      System.out.println(response.getStatus());
    } catch (Exception e) {
      throw new SocialAuthException("Error while getting profile from " + PROFILE_URL, e);
    }
    try {
      // System.out.println("User Profile : " + presp);
      Map<String, Object> data = Json.fromJson(Map.class, presp);
      if (!data.containsKey("id")) throw new SocialAuthException("Error: " + presp);
      if (userProfile == null) userProfile = new Profile();
      userProfile.setValidatedId(data.get("id").toString());
      userProfile.setProviderId(getProviderId());
      return userProfile;

    } catch (Exception ex) {
      throw new ServerDataException("Failed to parse the user profile json : " + presp, ex);
    }
  }