Twitter API failing with Oauth1.0 authentication

Viewed 107

I was trying to Post from Twitter so I have to generate oauth1.0 signature , so I tried to implement it

 public static String prepareParameterString(Authorization authorization) {
        StringBuilder builder = new StringBuilder();
        builder.append("include_entities=true").
                append("&").append("oauth_consumer_key=").append(percentEncode(authorization.getOauthCustomerKey())).
                append("&").append("oauth_nonce=").append(percentEncode(authorization.getOauthNonce()))
                .append("&").append("oauth_signature_method=").append(percentEncode(authorization.getOauthSignatureMethod()))
                .append("&").append("oauth_timestamp=").append(percentEncode(authorization.getOauthTimeStamp()))
                .append("&").append("oauth_token=").append(percentEncode(authorization.getOauthTokenKey()))
                .append("&").append("oauth_version=").append(percentEncode(authorization.getOauthVersion()))
                .append("&").append("status=").append(percentEncode(authorization.getStatus()));
        return builder.toString();
}

so this is the method by which I have created the base string

public static String prepareSignatureBaseString(String paramaterString, String httpMethod, String url) {
        return httpMethod + "&" + percentEncode(url) + "&" + percentEncode(paramaterString);
    }

public static String calculateSigningKey(Authorization authorization) {
    return percentEncode(Objects.requireNonNull(authorization.getOauthConsumerSecret())) + "&" +
            percentEncode(Objects.requireNonNull(authorization.getOauthTokenSecret()));
}


public static String calculateHMAC(String input, String signingKey) {
    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(signingKey.getBytes(),
                HMAC_SHA1_ALGORITHM);

        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(secretKeySpec);
        return Base64.getEncoder().encodeToString(mac.doFinal(input.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

so these were the method used to create signature key and hash

 public static String generateTimeStamp() {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());


        return (timestamp.getTime() / 1000L) + "";
    }

    public static String generateNonce() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String percentEncode(String string) {

        return new PercentEscaper("-_.~").escape(string);
    }

and these were to code to generate nonce , Timestamp , percent encode which is from guava library

 public static String getAuthorizationHeader(Signature status) {
        StringBuilder builder = new StringBuilder();
        appendParameter(builder, "oauth_consumer_key", status.getAuthorization().getOauthCustomerKey());
        appendParameter(builder, "oauth_nonce", status.getAuthorization().getOauthNonce());
        appendParameter(builder, "oauth_signature", status.getSignature());
        appendParameter(builder, "oauth_signature_method", status.getAuthorization().getOauthSignatureMethod());
        appendParameter(builder, "oauth_timestamp", status.getAuthorization().getOauthTimeStamp());
        appendParameter(builder, "oauth_token", status.getAuthorization().getOauthTokenKey());
        appendParameter(builder, "oauth_version", status.getAuthorization().getOauthVersion());


        System.out.println(builder.toString());
        return builder.substring(0, builder.length() - 1);

    }

    private static void appendParameter(StringBuilder buf, String name, String value) {
        if (value != null) {
            buf.append(' ').append(percentEncode(name)).append("=\"").append(percentEncode(value)).append("\",");
        }

    }

I have done everything as per the documents even the test cases run successfully with the data provided by twitter

@Test
    void urlEncoderTest2() {
        assertEquals("Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request%21",
                SecurityUtils.percentEncode("Hello Ladies + Gentlemen, a signed OAuth request!"));
    }

    @Test
    void securityTest() {
        Authorization authorization = new Authorization("xvz1evFS4wEEPTGEFPHBog",
                "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw",
                "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb",
                "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE", "Hello Ladies + Gentlemen, a signed OAuth request!");
        Signature signature = new Signature(authorization);
        authorization.setOauthNonce("kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg");
        authorization.setOauthTimeStamp("1318622958");
        assertEquals("hCtSmYh+iHYCEqBWrE7C7hYmtUk=" ,signature.generateSignature());
    }

I send it using OAuth header but getting 400 bad

{
"errors": [
  {
"code": 215,
"message": "Bad Authentication data."
}
],
}

can somebody tell me where I missed thanks in advance

1 Answers
Related