public void testInfo() {

    OAuthV1 oAuth = OauthKeeper.readOAuth(this);
    UserAPI userAPI = new UserAPI(OAuthConstants.OAUTH_VERSION_1);
    try {
      String response = userAPI.info(oAuth, "json"); // 获取用户信息
      Log.e("debug", "response:" + response);
    } catch (Exception e) {
      e.printStackTrace();
    }
    userAPI.shutdownConnection();
  }
Exemplo n.º 2
0
  @Override
  public Map queryUserInfoByCode(String code, String openid, String openkey) {
    HashMap map = new HashMap();
    OAuthV2 oAuth = TencentSociaTool.getQQAuthV2();
    oAuth.setAuthorizeCode(code);
    oAuth.setOpenid(openid);
    oAuth.setOpenkey(openkey);

    String accessToken = null, userOpenID = null;
    long tokenExpireIn = 0L;

    // 换取access token
    oAuth.setGrantType("authorize_code");
    try {
      OAuthV2Client.accessToken(oAuth);
    } catch (Exception e1) {
      e1.printStackTrace();
    }

    // 检查是否正确取得access token
    if (oAuth.getStatus() == 3) {
      System.out.println("Get Access Token failed!");
      return null;
    }

    UserAPI userAPI = new UserAPI(oAuth.getOauthVersion());
    String userInfoJson = "";
    try {
      userInfoJson = userAPI.info(oAuth, "json");
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Object userInfoObject = TencentSociaTool.getJsonDataObject(userInfoJson);
    if (judgeNull(userInfoObject)) {
      return null;
    }

    JSONObject userInfo = ((JSONObject) userInfoObject);
    map.put("user", userInfo);
    map.put("token", accessToken);
    map.put("oAuth", oAuth);
    map.put("code", code);
    return map;
  }