1
String baseString="POST&";
String subBaseString = "oauth_consumer_key="+oauth_consumer_key+"&oauth_nonce="+nonce+"&oauth_signature_method="+oauth_signature_method;
subBaseString += "&oauth_timestamp="+  oauth_timestamp+"&oauth_token="+oauth_token+"&oauth_version=1.0";
baseString += URLEncoder.encode(baseRequest, "UTF-8");
baseString += "&" +  URLEncoder.encode(subBaseString, "UTF-8");

String result;
try {

    SecretKeySpec signingKey = new SecretKeySpec(oauth_consumer_key.getBytes(), oauth_signature_method);

    Mac mac = Mac.getInstance(oauth_signature_method);
    mac.init(signingKey);

    byte[] rawHmac = mac.doFinal(baseString.getBytes());

    // base64-encode the hmac
    result = Base64.encode(rawHmac);

} catch (Exception e) {
    throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}

これは私の oauth_signature 生成コードです....

しかし、エラーが発生します..

{"エラー": "API v1+ の OAuthError。リクエストの署名が間違っています: 無効または署名がありません"}

4

1 に答える 1

2

OAuth署名、ナンス、タイムスタンプはすべて、プレーンHTTPを使用している場合に必要なセキュリティ対策です。ただし、Dropbox APIはHTTPS経由で使用できるため、その複雑さをすべて解消し、PLAINTEXT署名モードを使用する方がはるかに簡単です。

これがその仕事をするJavaコードの例です。(OAuth情報は「Authorization」HTTPヘッダーに配置されますが、必要に応じて代わりにURLパラメーターを使用できます。)

 /**
  * @param token
  *    For all "real" API endpoints, pass in the access token here.
  *    For "/oauth/access_token", pass in the request token here.
  *    (For "/oauth/request_token", use {@link #buildInitialOAuthHeader}.)
  */
 public static HttpHeader buildOAuthHeader(AppInfo appInfo, Token token)
 {
     StringBuilder buf = new StringBuilder();
     buf.append("OAuth ");
     buf.append("oauth_token=\"").append(token.key).append("\"");
     buf.append(", oauth_consumer_key=\"").append(appInfo.key).append("\"");
     buf.append(", oauth_signature_method=\"PLAINTEXT\"");
     buf.append(", oauth_signature=\"").append(appInfo.secret).append("&").append(token.secret).append("\"");
     return new HttpHeader("Authorization", buf.toString());
 }

 /**
  * For "/oauth/request_token".
  */
 public static HttpHeader buildInitialOAuthHeader(AppInfo appInfo)
 {
     StringBuilder buf = new StringBuilder();
     buf.append("OAuth ");
     buf.append(" oauth_consumer_key=\"").append(appInfo.key).append("\"");
     buf.append(", oauth_signature_method=\"PLAINTEXT\"");
     buf.append(", oauth_signature=\"").append(appInfo.secret).append("&\"");
     return new HttpHeader("Authorization", buf.toString());
 }
于 2012-06-08T03:14:03.570 に答える