Why are you doing this?....

StringBuilder params = new StringBuilder();
               params.append(encode("oauth_consumer_key"));
               params.append("=\"");
               params.append(encode(CONSUMER_KEY));
               params.append("\", ");
               params.append(encode("oauth_signature_method"));
               params.append("=\"");
               params.append(encode("HMAC-SHA1"));
               params.append("\", ");
               params.append(encode("oauth_signature"));
               params.append("=\"");
               params.append(encode(sig));
               params.append("\", ");
               params.append(encode("oauth_timestamp"));
               params.append("=\"");
               params.append(encode(Long.toString(timestamp)));
               params.append("\", ");
               params.append(encode("oauth_nonce"));
               params.append("=\"");
               params.append(encode(Long.toString(timestamp +
tmp.nextInt(1000))));
               params.append("\", ");
               params.append(encode("oauth_version"));
               params.append("=\"");
               params.append(encode("1.0"));
               params.append("\"");

Are you putting quotation marks around the values?

Ryan

On Wed, Feb 17, 2010 at 2:37 PM, Berto <mstbe...@gmail.com> wrote:

> Hey guys,
>
> I'm writing a client in java and trying to use oauth to get an access
> token.  However, I keep getting an IOException which essentially means
> I'm getting an HTTP 401 error back (unauthorized).  I've verified that
> my signature algorithm is correct by using some provided examples over
> at oauth.net, but nothing seems to be working for me.  Does the
> consumer key need an & after it?  I'm using the exact values provided
> via the register oauth client page.  Here's a snippet of the code:
>
> HttpURLConnection connection = null;
>            BufferedReader reader = null;
>            StringBuilder responseBuilder;
>            Date date = new Date();
>            long time = date.getTime();
>            long timestamp = time / 1000;
>            Random tmp = new Random();
>
>            try {
>                StringBuilder stuff = new StringBuilder();
>                stuff.append(encode("oauth_consumer_key"));
>                stuff.append("=");
>                stuff.append(encode(CONSUMER_KEY));
>                stuff.append("&");
>                stuff.append(encode("oauth_nonce"));
>                stuff.append("=");
>                stuff.append(encode(Long.toString(timestamp +
> tmp.nextInt(1000))));
>                stuff.append("&");
>                stuff.append(encode("oauth_signature_method"));
>                stuff.append("=");
>                stuff.append(encode("HMAC-SHA1"));
>                stuff.append("&");
>                stuff.append(encode("oauth_timestamp"));
>                stuff.append("=");
>                stuff.append(encode(Long.toString(timestamp)));
>                stuff.append("&");
>                stuff.append(encode("oauth_version"));
>                stuff.append("=");
>                stuff.append(encode("1.0"));
>
>                StringBuffer base = new
> StringBuffer("GET").append("&")
>                .append(encode("http://twitter.com/oauth/
> request_token")).append("&");
>                base.append(encode(stuff.toString()));
>                String oauthBaseString = base.toString();
>
>                String sig = signature(oauthBaseString,
> CONSUMER_SECRET);
>
>                StringBuilder params = new StringBuilder();
>                params.append(encode("oauth_consumer_key"));
>                params.append("=\"");
>                params.append(encode(CONSUMER_KEY));
>                params.append("\", ");
>                params.append(encode("oauth_signature_method"));
>                params.append("=\"");
>                params.append(encode("HMAC-SHA1"));
>                params.append("\", ");
>                params.append(encode("oauth_signature"));
>                params.append("=\"");
>                params.append(encode(sig));
>                params.append("\", ");
>                params.append(encode("oauth_timestamp"));
>                params.append("=\"");
>                params.append(encode(Long.toString(timestamp)));
>                params.append("\", ");
>                params.append(encode("oauth_nonce"));
>                params.append("=\"");
>                params.append(encode(Long.toString(timestamp +
> tmp.nextInt(1000))));
>                params.append("\", ");
>                params.append(encode("oauth_version"));
>                params.append("=\"");
>                params.append(encode("1.0"));
>                params.append("\"");
>
>                // Prepare the connection
>                URL url = new URL("http://twitter.com/oauth/
> request_token");
>                connection = (HttpURLConnection) url.openConnection();
>                connection.setRequestMethod("GET");
>
>                connection.setRequestProperty("WWW-Authenticate",
> "OAuth " + params.toString());
>
>                connection.setConnectTimeout(30000);
>                connection.setReadTimeout(30000);
>
>                // Read the response
>               int code = -1;
>                try {
>                code = connection.getResponseCode();
>                } catch (IOException e) {
>                   e.printStackTrace();
>                }
>                reader = new BufferedReader(new
> InputStreamReader(connection.getInputStream()));
>                responseBuilder = new StringBuilder();
>
>                String line = null;
>                while ((line = reader.readLine()) != null) {
>                    responseBuilder.append(line);
>                }
>

Reply via email to