2
static public final String CUSUMER_KEY = "mcIgMJKcyhqFlAHK9batPvRvC";
static public final String CUSUMER_SECRET = "5PRVECiRPoddsBd70qGBfZZdJVPwWcxiicFf7usfS5nyhg4GGp";
static public final String ACCESS_TOKEN = "2320954854-eZYObTlxJ3Pqqd0YuGNlWcRQJQ4rNAyvsfsssV7";
static public final String ACCESS_TOKEN_SECRET = "iOEYAG5yEgRuWRh8MY5AIfwK2f2oF6HF9gAkzArTLQUKS";
static public final String REQUEST_URL = "https://api.twitter.com/oauth/request_token";
static public final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
static public final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
static public final String AUTH_URL = "https://api.twitter.com/oauth/authenticate?oauth_token=";
static public final String CALL_BACK = "%22http%3A%2F%2Fmyapp.com%3A3005%2Ftwitter%2Fprocess_callback%22";
public String get_access_token(){
    String ret="";
    try{
        String sg_base_str = "POST&"+ URLEncoder.encode("https://api.twitter.com/oauth/request_token", "UTF-8")+"&";
        String[][] data =
                {
                        {"oauth_consumer_key"    , CUSUMER_KEY  },
                        {"oauth_nonce"           , oauth_nounce()          },
                        {"oauth_signature"       , ""                       },
                        {"oauth_signature_method", "HMAC-SHA1"        },
                        {"oauth_timestamp"       , oauth_timestamp()        },
                        {"oauth_token"           , ACCESS_TOKEN        },
                        {"oauth_version"         , "1.0"       }
                };
        for(int i = 0; i < data.length; i++)
        {
            String _26;

            if(i + 1 < data.length) {
                _26 = "%26";
            }
            else _26 = "";

            if(data[i][0] != "oauth_signature")
            {
                sg_base_str += URLEncoder.encode(data[i][0],"UTF-8") + "%3D"
                        +  URLEncoder.encode(data[i][1],"UTF-8") + _26 ;
            }
        }

        ret =  hmacDigest(sg_base_str,CUSUMER_SECRET+"&"+ACCESS_TOKEN_SECRET, "HmacSHA1")+"\n\n"+sg_base_str;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    ret=ret.replaceAll("/","%2F");
    ret=ret.replaceAll("=","%3D");
    ret=ret.replaceAll("\\+","%2B");
    return ret;
}

public String oauth_nounce() {
    String uuid_string = UUID.randomUUID().toString();
    uuid_string = uuid_string.replaceAll("-", "");
    String oauth_nonce = uuid_string;
    return oauth_nonce;
}

public String oauth_timestamp() {
    long millis = System.currentTimeMillis();
    long secs   = millis / 1000;

    return String.valueOf(secs);
}`public static String hmacDigest(String msg, String keyString, String algo) {
    String digest = null;
    try {
        SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), algo);
        Mac mac = Mac.getInstance(algo);
        mac.init(key);

        byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));

        String encoded = Base64.encodeToString(bytes, 0);
        digest = encoded;
    } catch (UnsupportedEncodingException e) {
    } catch (InvalidKeyException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    return digest;
}`
  1. When I run code to get this: oauth_nonce=252e7f4eee134ac2bf8cc287620ec2d9 oauth_timestamp=1433957398 oauth_signature=vAcm%2FNyc7S1RISe6jk7y8N1knMo%3D

  2. Then I has POST https://api.twitter.com/oauth/request_token?oauth_consumer_key=mcIgMJKcyhqFlAHK9batPvRvC&oauth_nonce=252e7f4eee134ac2bf8cc287620ec2d9&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1433957398&oauth_token=2320954854-eZYObTlxJ3Pqqd0YuGNlWcRQJQ4rNAyvsfsssV7&oauth_version=1.0&oauth_signature=vAcm%2FNyc7S1RISe6jk7y8N1knMo%3D

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

Tell me why?

  • Is there a reason you are manually creating the signature instead of using an existing OAuth library? OAuth 1 encoding/signing is notoriously complex. – abraham Jun 10 '15 at 22:50
  • Any luck yet??? – clankill3r May 4 '16 at 14:55
1

Try changing all the "http" 's to "https".

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.