Search code examples
javatwitteroauth

Twitter. Verify_credentials. OAuth Api. Error "Could not authenticate you." Include_email=true


If I include parameter Include_email=true I got an error:

"Could not authenticate you."

Here the code. Please, help.

public final static String VERIFY_CREDENTIALS_URL ="https://api.twitter.com/1.1/account/verify_credentials.json";

    public final static String AUTHORIZATION_VERIFY_CREDENTIALS = "OAuth " +
            "oauth_consumer_key=\"{key}\", " +
            "oauth_signature_method=\"" + SIGNATURE_METHOD + "\", " +
            "oauth_timestamp=\"{ts}\", " +
            "oauth_nonce=\"{nonce}\", " +
            "oauth_version=\"1.0\", " +
            "oauth_signature=\"{signature}\", " +
            "oauth_token=\"{token}\"";

    public static TwitterVerifyCredentials getVerifyCredentials(String appKey, String appSecret, TwitterAccessToken accessToken) throws GeneralSecurityException, IOException {
        String ts = "" + TimeUtility.now().getTime()/1000;
        String oauth_nonce = UUID.randomUUID().toString().replaceAll("-", "");
        String parameters = "oauth_consumer_key=" + appKey + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + SIGNATURE_METHOD + "&oauth_timestamp=" + ts + "&oauth_token=" + encode(accessToken.getToken()) + "&oauth_version=1.0";
        String signature = "GET&" + encode(VERIFY_CREDENTIALS_URL) + "&" + encode(parameters);
        System.out.println(signature);

        String result = TwitterOAuth.AUTHORIZATION_VERIFY_CREDENTIALS;
        result = StringUtils.replace(result, "{nonce}", oauth_nonce);
        result = StringUtils.replace(result, "{ts}", "" + ts);
        result = StringUtils.replace(result, "{key}", appKey);
        result = StringUtils.replace(result, "{signature}", encode(computeSignature(signature, appSecret + "&" + encode(accessToken.getTokenSecret()))));
        result = StringUtils.replace(result, "{token}", encode(accessToken.getToken()));

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", result);

        String s = SiteFunctions.urlGet(VERIFY_CREDENTIALS_URL+"?include_email=true", headers);
        System.out.println(s);
        return new TwitterVerifyCredentials(); // while testing
    }

If I remove ?include_email=true everything is ok!


Solution

  • VERIFY_CREDENTIALS_URL in your program should add with ?include_email=true. If you want to get email from Twitter OAuth api, the url in signature should have ?include_email=true.