Mahmoud Ibrahim
Mahmoud Ibrahim

Reputation: 1085

Error message "Could not authenticate you" with code 32 when calling twitter API?

When calling this api: https://api.twitter.com/1.1/followers/list.json?amp%3Binclude_user_entities=false&amp%3Bscreen_name=twitterdev&amp%3Bskip_status=true&cursor=-1

with the following Authorization:

OAuth oauth_consumer_key="XXXXXXXXXXXXXXXXXXXXXXXXX", oauth_nonce="0ddddff20481ae3567739f4eefa49b22", oauth_signature="7DQKPmbqJAzRh%2FM0UTB2w82lGvY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1468795885", oauth_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", oauth_version="1.0" 

to get my followers list from twitter, I get the json object of my followers successfully, but when replacing the value of the screen_name (parameter passed with the top api) from twitterdev to my actual screen_name's value, I get this error:

{
"errors": [
    {
        "code": 32,
        "message": "Could not authenticate you."
    }
]

}

Please, Can any one help me to solve this problem ?


The following code to generate an OAuth Signature for my request:

    String oauth_signature_method = "HMAC-SHA1";
    String oauth_consumer_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    String oauth_token= "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    String uuid_string = UUID.randomUUID().toString();
    uuid_string = uuid_string.replaceAll("-", "");
    String oauth_nonce = uuid_string;

    Date now = Calendar.getInstance().getTime();
    String oauth_timestamp = (new Long(now.getTime()/1000)).toString();

    String parameter_string = "oauth_consumer_key=" + oauth_consumer_key + "&oauth_nonce=" +
            oauth_nonce + "&oauth_signature_method=" + oauth_signature_method +
            "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + authToken +
            "&oauth_version=1.0";

    String signature_base_string = " GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Ffollowers%2Flist.json&"+
            URLEncoder.encode("user_id=XXXXXXXXXX&", "UTF-8") +
            URLEncoder.encode(parameter_string, "UTF-8");

    String oauth_signature = computeSignature(signature_base_string, "XXXXXXXXXXXXXXXXXXXXXXXXXXX" + "&");

    String auth_header_string = "OAuth oauth_consumer_key=\"" + oauth_consumer_key +
            "\", oauth_nonce=\"" + oauth_nonce +
            "\", oauth_signature=\""+URLEncoder.encode(oauth_signature, "UTF-8") +
            "\", oauth_signature_method=\"HMAC-SHA1" +
            "\", oauth_timestamp=\""+ oauth_timestamp +
            "\", oauth_token=\""+oauth_token +
            "\", oauth_version=\"1.0\"";


private String computeSignature(String baseString, String keyString){

    SecretKey secretKey = null;

    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");

    Mac mac = null;
    try {
        mac = Mac.getInstance("HmacSHA1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    try {
        mac.init(secretKey);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    byte[] text = baseString.getBytes();

    return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}

Upvotes: 0

Views: 1761

Answers (1)

Yuri Schimke
Yuri Schimke

Reputation: 13458

You can't change the request query parameters without regenerating a new signature. See https://dev.twitter.com/oauth/overview/creating-signatures specifically "Collecting parameters".

There are plenty of examples in stackoverflow for signing requests, you should start there, specifically search for the frameworks you are using in Android.

Upvotes: 1

Related Questions