예제 #1
0
  /**
   * 处理签名请求和参数
   *
   * @param url
   * @param httpMethod
   * @param parameters
   * @return
   */
  private String generateSignatureBase(URL url, String httpMethod, List<QParameter> parameters) {

    StringBuilder base = new StringBuilder();
    base.append(httpMethod.toUpperCase());
    base.append("&");
    base.append(QStr.encode(getNormalizedUrl(url)));
    base.append("&");
    base.append(QStr.encode(encodeParams(parameters)));

    return base.toString();
  }
예제 #2
0
 /**
  * 编码请求参数
  *
  * @param params
  * @return
  */
 private static String encodeParams(List<QParameter> params) {
   StringBuilder result = new StringBuilder();
   for (QParameter param : params) {
     if (result.length() != 0) {
       result.append("&");
     }
     result.append(QStr.encode(param.getName()));
     result.append("=");
     result.append(QStr.encode(param.getValue()));
   }
   return result.toString();
 }
예제 #3
0
 /**
  * 生成签名值
  *
  * @param base
  * @param consumerSecret
  * @param accessTokenSecret
  * @return
  */
 private String generateSignature(String base, String consumerSecret, String accessTokenSecret) {
   try {
     Mac mac = Mac.getInstance(hashAlgorithmName);
     String oauthSignature =
         QStr.encode(consumerSecret)
             + "&"
             + ((accessTokenSecret == null) ? "" : QStr.encode(accessTokenSecret));
     SecretKeySpec spec = new SecretKeySpec(oauthSignature.getBytes(), hashAlgorithmName);
     mac.init(spec);
     byte[] bytes = mac.doFinal(base.getBytes());
     return new String(Base64Encoder.encode(bytes));
   } catch (Exception e) {
   }
   return null;
 }
예제 #4
0
  /**
   * 处理请求参数 和 生成签名
   *
   * @param url
   * @param httpMethod
   * @param consumerSecret
   * @param tokenSecrect
   * @param parameters
   * @return
   */
  public String getOauthParams(
      String url,
      String httpMethod,
      String consumerSecret,
      String tokenSecrect,
      List<QParameter> parameters) {
    Collections.sort(parameters);

    String urlWithParameter = url;

    String parameterString = encodeParams(parameters);
    if (parameterString != null && !parameterString.equals("")) {
      urlWithParameter += "?" + parameterString;
    }

    URL aUrl = null;
    try {
      aUrl = new URL(urlWithParameter);
    } catch (MalformedURLException e) {
      System.err.println("URL parse error:" + e.getLocalizedMessage());
    }

    String signature =
        this.generateSignature(aUrl, consumerSecret, tokenSecrect, httpMethod, parameters);

    parameterString += "&oauth_signature=";
    parameterString += QStr.encode(signature);

    return parameterString;
  }