예제 #1
0
  public String getToken(String code, String state, boolean sandbox) {
    String url =
        sandbox
            ? wiyReportConfiguration.getSandboxTokenUrl()
            : wiyReportConfiguration.getProductionTokenUrl();
    Map<String, String> props = new HashMap<String, String>();
    props.put("grant_type", "authorization_code");
    props.put("code", code);
    props.put(
        "client_id",
        sandbox
            ? wiyReportConfiguration.getSandboxAppKey()
            : wiyReportConfiguration.getProductionAppKey());
    props.put(
        "client_secret",
        sandbox
            ? wiyReportConfiguration.getSandboxAppSecret()
            : wiyReportConfiguration.getProductionAppSecret());
    props.put(
        "redirect_uri",
        sandbox
            ? wiyReportConfiguration.getSandboxCallbackUrl()
            : wiyReportConfiguration.getProductionCallbackUrl());
    props.put("view", "web");
    props.put("state", state);
    String jsonResponse = null;
    try {
      jsonResponse = WebUtils.doPost(url, props, 30000, 30000);
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }

    return jsonResponse;
  }
예제 #2
0
파일: TaobaoApiUtil.java 프로젝트: kkme/udc
 public static String refreshToken(
     String refreshTokenUrl, String appKey, String appSecret, String refreshToken)
     throws IOException, IncorrectInputParameterException, JSONException {
   Map<String, String> parameters = new HashMap<String, String>();
   parameters.put(UserTaobaoInfoKey.API_GRANT_TYPE, UserTaobaoInfoKey.API_REFRESH_TOKEN);
   parameters.put(UserTaobaoInfoKey.API_REFRESH_TOKEN, refreshToken);
   parameters.put(UserTaobaoInfoKey.API_CLIENT_ID, appKey);
   parameters.put(UserTaobaoInfoKey.API_CLIENT_SECRET, appSecret);
   parameters.put(UserTaobaoInfoKey.API_SCOPE, UserTaobaoInfoKey.API_SCOPE_VALUE);
   parameters.put(UserTaobaoInfoKey.API_VIEW, UserTaobaoInfoKey.API_VIEW_VALUE);
   String reponseStr =
       WebUtils.doPost(refreshTokenUrl, parameters, S.FIVE_SECONDS, S.FIVE_SECONDS);
   if (S.isInvalidValue(reponseStr)) {
     throw new IncorrectInputParameterException("Refresh token response return is null");
   }
   JSONObject responseObject = new JSONObject(reponseStr);
   String token = responseObject.getString(UserTaobaoInfoKey.API_ACCESS_TOKEN);
   if (token == null) {
     throw new IncorrectInputParameterException("Token return after refreshing is null");
   }
   return token;
 }
  public TaobaoItemRichInfo parseRichItemsJSON(String itemId) throws IOException {
    TaobaoItemBasicInfo taobaoItemBasicInfo = new TaobaoItemBasicInfo();
    TaobaoItemRichInfo taobaoItemRichInfo = new TaobaoItemRichInfo();
    String itemBasicJson = "";
    String itemRichJson = "";
    if (Helper.checkConnection(context)) {
      try {
        int timeout = 30000;
        Map param = new HashMap<String, String>();
        param.put("securityKey", SecurityKey.getKey());
        String itemBasicInfoUrl = Constants.SERVER_DOMAIN + "/api/item/basicinfo/" + itemId;
        String itemRichInfoUrl = Constants.SERVER_DOMAIN + "/api/item/picwordinfo/" + itemId;
        itemBasicJson = WebUtils.doPost(itemBasicInfoUrl, param, timeout, timeout);
        itemRichJson = WebUtils.doPost(itemRichInfoUrl, param, timeout, timeout);
      } catch (IOException e) {
        Log.e("IOException is : ", e.toString());
        e.printStackTrace();
        return taobaoItemRichInfo;
      }
    }
    try {
      if (null != itemBasicJson && !"".equals(itemBasicJson)) {
        JSONObject jsonObject = new JSONObject(itemBasicJson);
        String result = jsonObject.has("ret") ? jsonObject.get("ret").toString() : null;
        if (result == null || !result.contains("SUCCESS")) {
          return taobaoItemRichInfo;
        }
        JSONObject dateJsonObject = jsonObject.getJSONObject("data");
        String sellerJson = dateJsonObject.getString("seller");
        String rateJson = dateJsonObject.getString("rateInfo");
        JSONArray apiStackJsonArray = dateJsonObject.getJSONArray("apiStack");
        if (apiStackJsonArray == null || apiStackJsonArray.length() == 0) {
          return taobaoItemRichInfo;
        }
        JSONObject esiInfoObject = new JSONObject(apiStackJsonArray.get(0).toString());
        JSONObject apiStackDataObject =
            new JSONObject(esiInfoObject.getString("value")).getJSONObject("data");
        JSONObject itemInfoObject = dateJsonObject.getJSONObject("itemInfoModel");

        JSONArray picArray = itemInfoObject.getJSONArray("picsPath");
        // 这里为商品列表页展示数据使用,所以只展示一张图片
        taobaoItemBasicInfo.setItemId(Long.valueOf(itemId));
        taobaoItemBasicInfo.setTitle(itemInfoObject.getString("title"));
        taobaoItemBasicInfo.setFavcount(itemInfoObject.getString("favcount"));
        taobaoItemBasicInfo.setSku(itemInfoObject.getBoolean("sku"));
        taobaoItemBasicInfo.setItemUrl(itemInfoObject.getString("itemUrl"));
        taobaoItemBasicInfo.setLocation(itemInfoObject.getString("location"));
        ArrayList picsPath = new ArrayList<String>();
        for (int i = 0; i < picArray.length(); i++) {
          picsPath.add(picArray.getString(i));
        }
        taobaoItemBasicInfo.setPicsPath(picsPath);
        taobaoItemBasicInfo.setSellerInfo(new SellerInfo(sellerJson));
        taobaoItemBasicInfo.setRateInfo(new RateInfo(rateJson));

        if (taobaoItemBasicInfo.getSku()) {
          JSONObject skuModelObject = dateJsonObject.getJSONObject("skuModel");
          taobaoItemBasicInfo.setSkuModel(new SkuModel(skuModelObject, apiStackDataObject));
        } else {
          taobaoItemBasicInfo.setSkuModel(new SkuModel(null, apiStackDataObject));
        }
      }
      if (null != itemRichJson && !"".equals(itemRichJson)) {
        JSONObject jsonObject = new JSONObject(itemRichJson);
        String result = jsonObject.get("ret").toString();
        if (result == null || !result.contains("SUCCESS")) {
          return taobaoItemRichInfo;
        }
        JSONArray imagesJsonArray = jsonObject.getJSONObject("data").getJSONArray("images");
        List imagesList = new ArrayList();
        for (int i = 0; i < imagesJsonArray.length(); i++) {
          imagesList.add(imagesJsonArray.get(i).toString());
        }
        taobaoItemRichInfo.setImageList(imagesList);
      }
      taobaoItemRichInfo.setBasicInformation(taobaoItemBasicInfo);
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return taobaoItemRichInfo;
  }
예제 #4
0
  public <T extends TaobaoResponse> Map<String, Object> doPost(
      TaobaoRequest<T> request, String accessToken, String version) throws AlipayApiException {
    Map<String, Object> result = new HashMap<String, Object>();
    RequestParametersHolder requestHolder = new RequestParametersHolder();
    TaobaoHashMap appParams = new TaobaoHashMap(request.getTextParams());
    requestHolder.setApplicationParams(appParams);

    TaobaoHashMap protocalMustParams = new TaobaoHashMap();
    protocalMustParams.put(AlipayConstants.METHOD, request.getApiMethodName());
    protocalMustParams.put(AlipayConstants.VERSION, version);
    protocalMustParams.put(AlipayConstants.APP_ID, this.appId);

    Long timestamp = System.currentTimeMillis();
    DateFormat df = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
    df.setTimeZone(TimeZone.getTimeZone(Constants.DATE_TIMEZONE));
    protocalMustParams.put(AlipayConstants.TIMESTAMP, df.format(new Date(timestamp)));
    requestHolder.setProtocalMustParams(protocalMustParams);

    TaobaoHashMap protocalOptParams = new TaobaoHashMap();
    protocalOptParams.put(AlipayConstants.FORMAT, format);
    protocalOptParams.put(AlipayConstants.SIGN_TYPE, this.sign_type);
    protocalOptParams.put(AlipayConstants.ACCESS_TOKEN, accessToken);
    protocalOptParams.put(AlipayConstants.ALIPAY_SDK, Constants.SDK_VERSION);
    requestHolder.setProtocalOptParams(protocalOptParams);

    if (AlipayConstants.SIGN_TYPE_RSA.equals(this.sign_type)) {

      String signContent = AlipaySignature.getSignatureContent(requestHolder);

      protocalMustParams.put(
          AlipayConstants.SIGN,
          AlipaySignature.rsaSign(signContent, privateKey, Constants.CHARSET_UTF8));

    } else {
      protocalMustParams.put(AlipayConstants.SIGN, "");
    }

    StringBuffer urlSb = new StringBuffer(serverUrl);
    try {
      String sysMustQuery =
          WebUtils.buildQuery(requestHolder.getProtocalMustParams(), Constants.CHARSET_UTF8);
      String sysOptQuery =
          WebUtils.buildQuery(requestHolder.getProtocalOptParams(), Constants.CHARSET_UTF8);

      urlSb.append("?");
      urlSb.append(sysMustQuery);
      if (sysOptQuery != null & sysOptQuery.length() > 0) {
        urlSb.append("&");
        urlSb.append(sysOptQuery);
      }
    } catch (IOException e) {
      throw new AlipayApiException(e);
    }

    String rsp = null;
    try {
      if (request instanceof TaobaoUploadRequest) {
        TaobaoUploadRequest<T> uRequest = (TaobaoUploadRequest<T>) request;
        Map<String, FileItem> fileParams = TaobaoUtils.cleanupMap(uRequest.getFileParams());
        rsp = WebUtils.doPost(urlSb.toString(), appParams, fileParams, connectTimeout, readTimeout);
      } else {
        rsp = WebUtils.doPost(urlSb.toString(), appParams, connectTimeout, readTimeout);
      }
    } catch (IOException e) {
      throw new AlipayApiException(e);
    }
    result.put("rsp", rsp);
    result.put("textParams", appParams);
    result.put("protocalMustParams", protocalMustParams);
    result.put("protocalOptParams", protocalOptParams);
    result.put("url", urlSb.toString());
    return result;
  }