Ejemplo n.º 1
0
 /** 接收腾讯微博授权的返回数据 */
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   // TODO Auto-generated method stub
   if (requestCode == REQUEST_CODE) { // 对应之前设置的的myRequsetCode
     if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
       // 取得返回腾讯的OAuthV2类实例,再转成通用token并存储
       tencentAuthV2 = (OAuthV2) data.getExtras().getSerializable("oauth");
       CommonOAuth2AccessToken token = new CommonOAuth2AccessToken();
       token.setToken(tencentAuthV2.getAccessToken());
       // 腾讯的过期时间需要转换
       token.setExpiresIn(
           System.currentTimeMillis() + Long.parseLong(tencentAuthV2.getExpiresIn()) * 1000 + "");
       token.setOpenId(tencentAuthV2.getOpenid());
       token.setOpenKey(tencentAuthV2.getOpenkey());
       AccessTokenKeeper.keepAccessToken(
           this, token, AccessTokenKeeper.PREFERENCES_NAME_TOKEN_TENCENT);
       // 更改运行时数据
       RunningData.tencentTokenState = Constants.oauth.HAS_AUTHORIZED;
       RunningData.tencentAccessToken = token;
       // 更改运行时腾讯接口调用对象
       RunningData.oAuthV2 =
           new OAuthV2(
               Constants.tencent.clientId,
               Constants.tencent.clientSecret,
               Constants.tencent.redirectUri);
       RunningData.oAuthV2.setAccessToken(token.getToken());
       RunningData.oAuthV2.setOpenid(token.getOpenId());
       RunningData.oAuthV2.setOpenkey(token.getOpenKey());
       // 发送更新UI的消息
       tencentOAuthCallbackHandler.sendEmptyMessage(TENCENT_OAUTH_SUCCESS);
     }
   }
 }
Ejemplo n.º 2
0
  public OAuthV2 queryAccountToken(String accountId) {
    // 取数据库中的accesToken和openId
    SocialUserAccountInfo tok =
        socialUserAccountInfoService.findAccountOfInfoByKey(DictDef.dict("accessToken"), accountId);
    String assessToken = tok.getValue();
    SocialUserAccountInfo openIdInfo =
        socialUserAccountInfoService.findAccountOfInfoByKey(DictDef.dict("openId"), accountId);
    String openId = openIdInfo.getValue();

    OAuthV2 oAuth = TencentSociaTool.getQQAuthV2();
    oAuth.setAccessToken(assessToken);
    oAuth.setOpenid(openId);
    return oAuth;
  }
Ejemplo n.º 3
0
 @Override
 public void sendWeibo(SocialUserAccount socialUserAccount, String text, String type) {
   String accountId = socialUserAccount.getId();
   OAuthV2 oAuth = queryAccountToken(accountId);
   int ret = 0;
   TAPI tAPI = new TAPI(oAuth.getOauthVersion()); // 根据oAuth配置对应的连接管理器
   try {
     String response = tAPI.add(new AddParameter(oAuth, text));
     ret = 1;
   } catch (Exception e) {
     e.printStackTrace();
   }
   if (ret == 1) {
     saveSendWeiboRecord(socialUserAccount, text, type);
   }
 }
Ejemplo n.º 4
0
 /**
  * 我的所有微博
  *
  * @param ac
  * @param oAuth
  * @param pBroadcastTime
  */
 public void homeWeiboToDb(
     SocialUserAccount ac, OAuthV2 oAuth, TimelineParameter pBroadcastTime, boolean sign) {
   String response = "";
   try {
     response = new StatusesAPI(oAuth.getOauthVersion()).homeTimeline(pBroadcastTime);
   } catch (Exception e) {
     e.printStackTrace();
   }
   if (StringTool.judgeBlank(response)) {
     WeiboHandleResult r = weiboSaveDb(response, ac, DictDef.dictInt("weibo_public_timeline"));
     if (r.isSuccess() && r.getInfoSize() > 0) {
       pBroadcastTime.lastid = r.getLastid();
       pBroadcastTime.pagetime = r.getPagetime();
       if (!sign) { // 保存微博的最后时间
         sign = true;
         SocialUserAccountInfo info = new SocialUserAccountInfo();
         info.setKey("weibo_last_time"); // 微博最后更新时间
         Date d = new Date(Long.parseLong(r.getPagetime()) * 1000);
         info.setAccountId(ac.getId());
         info.setType(DictDef.dict("user_account_info_type")); // 帐号类型
         info.setValue(StringDateUtil.parseString(d, 4));
         info.setValueType(DictDef.dictInt("date"));
         info.setValueDate(d);
         socialUserAccountInfoService.createSocialUserAccountInfo(info);
       }
       homeWeiboToDb(ac, oAuth, pBroadcastTime, sign);
     }
   }
 }
Ejemplo n.º 5
0
 /** 评论 */
 @Override
 public int discussWeibo(WeiboForwardSend weiboForwardSend) {
   String accountId = weiboForwardSend.getUserAccountId();
   OAuthV2 oAuth = queryAccountToken(accountId);
   int ret = 0;
   TAPI tAPI = new TAPI(oAuth.getOauthVersion()); // 根据oAuth配置对应的连接管理器
   try {
     String response =
         tAPI.comment(
             new AddParameter(oAuth, weiboForwardSend.getContent(), weiboForwardSend.getWeibId()));
     boolean isRplyOk = JSONObject.fromObject(response).getString("msg").equals("ok");
     if (isRplyOk) {
       ret = 1;
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
   return ret;
 }
Ejemplo n.º 6
0
  private void addTokenInfos(OAuthV2 oAuth, SocialUserAccount socialUserAccount) {
    SocialUserAccountInfo info = newAccountInfo(socialUserAccount);
    info.setKey("accessToken");
    info.setValue(oAuth.getAccessToken());
    info.setValueType(DictDef.dictInt("string"));
    socialUserAccountInfoService.createSocialUserAccountInfo(info);

    String expireIn = oAuth.getExpiresIn();
    // 失效时间
    Date expiredTime = StringDateUtil.addSecond(new Date(), Integer.parseInt(expireIn));
    SocialUserAccountInfo expireInfo = newAccountInfo(socialUserAccount);
    expireInfo.setKey("expiredTime");
    expireInfo.setValue(expireIn);
    expireInfo.setValueDate(expiredTime);
    expireInfo.setValueType(DictDef.dictInt("date"));
    socialUserAccountInfoService.createSocialUserAccountInfo(expireInfo);

    SocialUserAccountInfo oInfo = newAccountInfo(socialUserAccount);
    oInfo.setKey("openId");
    oInfo.setValue(oAuth.getOpenid());
    oInfo.setValueType(DictDef.dictInt("string"));
    socialUserAccountInfoService.createSocialUserAccountInfo(oInfo);

    oInfo = newAccountInfo(socialUserAccount);
    oInfo.setKey("openKey");
    oInfo.setValue(oAuth.getOpenkey());
    oInfo.setValueType(DictDef.dictInt("string"));
    socialUserAccountInfoService.createSocialUserAccountInfo(oInfo);

    // refresh_token
    oInfo = newAccountInfo(socialUserAccount);
    oInfo.setKey("refreshToken");
    oInfo.setValue(oAuth.getRefreshToken());
    oInfo.setValueType(DictDef.dictInt("string"));
    socialUserAccountInfoService.createSocialUserAccountInfo(oInfo);
  }
Ejemplo n.º 7
0
 /**
  * 我发表的微博
  *
  * @param ac
  * @param oAuth
  * @param pBroadcastTime
  */
 public void myWeiboToDb(SocialUserAccount ac, OAuthV2 oAuth, TimelineParameter pBroadcastTime) {
   String response = "";
   try {
     response = new StatusesAPI(oAuth.getOauthVersion()).broadcastTimeline(pBroadcastTime);
   } catch (Exception e) {
     e.printStackTrace();
   }
   if (StringTool.judgeBlank(response)) {
     WeiboHandleResult r = weiboSaveDb(response, ac, DictDef.dictInt("weibo_user_timeline"));
     if (r.isSuccess() && r.getInfoSize() > 0) {
       pBroadcastTime.lastid = r.getLastid();
       pBroadcastTime.pagetime = r.getPagetime();
       myWeiboToDb(ac, oAuth, pBroadcastTime);
     }
   }
 }
Ejemplo n.º 8
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;
  }