Sending signed OAuth header:

Authorization: OAuth oauth_nonce="twitterecm1297372838828",
oauth_callback="http%3A%2F%2Ftwitter.myurl.com%2Fwf%2Fwf
%2Fservlet.method
%2Fcom.iflow.wf.cpn.twitter.TwitterTest.testCallback",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1297372838828",
oauth_consumer_key="05aXcuPKpDAQMR******",
oauth_signature="O%2F4RKChkbfXCt%2Bp73%2******",
oauth_version="1.0"

to URL: https://api.twitter.com/oauth/request_token

and get Respnoseheaders:

null:[HTTP/1.1 401 Unauthorized]
X-Runtime:[0.00684]
Content-Length:[44]
X-Transaction:[1297372843-99326-52182]
Expires:[Tue, 31 Mar 1981 05:00:00 GMT]
Last-Modified:[Thu, 10 Feb 2011 21:20:43 GMT]
X-Revision:[DEV]
Set-Cookie:
[_twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCJaOchEuAToHaWQiJTBmYWI3MGQ3ZTExYTYz
%250ANzc2ZTE4Y2Y3MWIwYzIwMjg5IgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy
%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--
ca7e3ee9b6aee2cb024341be086688bee84c3aac; domain=.twitter.com; path=/;
HttpOnly, guest_id=129737284366960543; path=/; expires=Sat, 12 Mar
2011 21:20:43 GMT, k=188.22.59.201.1297372843662357; path=/;
expires=Thu, 17-Feb-11 21:20:43 GMT; domain=.twitter.com]
Connection:[close]
Server:[hi]
Cache-Control:[no-cache, no-store, must-revalidate, pre-check=0, post-
check=0]
Pragma:[no-cache]
Status:[401 Unauthorized]
Date:[Thu, 10 Feb 2011 21:20:43 GMT]
Vary:[Accept-Encoding]
Content-Type:[text/html; charset=utf-8]

and when reading from the URL (opening input stream):
java.io.IOException: Server returned HTTP response code: 401 for URL:
https://api.twitter.com/oauth/request_token

Java Code:

        PrintWriter pw = res.getWriter();
        String timestamp = "" + new Date().getTime();
        String nonce = "twitterecm" + timestamp;

        String httpParameters = "oauth_callback=" +
URLEncoder.encode(TWITTER_CALLBACKURL, "UTF-8")
        + "&oauth_consumer_key=" + URLEncoder.encode(CONSUMER_KEY,
"UTF-8")
        + "&oauth_nonce=" + URLEncoder.encode(nonce, "UTF-8")
        + "&oauth_signature_method=HMAC-SHA1"
        + "&oauth_timestamp=" + timestamp
        + "&oauth_version=1.0";

        String signatureBase = "POST"
        + "&" + URLEncoder.encode(REQUESTTOKEN_ENDPOINT,
HttpUtils.UTF_8)
        + "&" + URLEncoder.encode(httpParameters, "UTF-8");

        pw.write("<br/><br/><b>SIGNATUREBASE</b>: " + signatureBase);

        String signatureKey = CONSUMER_SECRET + "&";

        pw.write("<br/><b>SIGNATUREKEY</b>: " + signatureKey);

        // sign URL with consumer secret
        String signature =  getSignature(signatureBase, signatureKey);

        pw.write("<br/><br/><b>SIGNATURE</b>: " + signature);


        String header = "OAuth oauth_nonce=\"" +
URLEncoder.encode(nonce, "UTF-8")
        + "\", oauth_callback=\"" +
URLEncoder.encode(TWITTER_CALLBACKURL, "UTF-8")
        + "\", oauth_signature_method=\"HMAC-SHA1"
        + "\", oauth_timestamp=\"" + timestamp
        + "\", oauth_consumer_key=\"" +
URLEncoder.encode(CONSUMER_KEY, "UTF-8")
        + "\", oauth_signature=\"" + URLEncoder.encode(signature,
HttpUtils.UTF_8)
        + "\", oauth_version=\"1.0"
        + "\"";

        pw.write("<br/><br/><b>HEADER:</b> Authorization: " +
header.replace(",", ",<br/>"));


        String urlStr = REQUESTTOKEN_ENDPOINT;

        pw.write("<br/><br/><b>TO URL</b>: " + urlStr);

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
        conn.setRequestProperty("Authorization", header);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        OutputStreamWriter writer = new
OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        writer.write(httpParameters);
        writer.flush();


        StringBuilder answer = new StringBuilder();
        BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream(), "UTF-8"));

        String line;
        while ((line = reader.readLine()) != null) {
            answer.append(line);
        }
        writer.close();
        reader.close();

        pw.write("<br/>" + answer.toString());

        pw.flush();
        pw.close();

Pleas help, tried lots of different things, Google API with
OAuthConsumer, OAuthProvider etc. too, got always the same response.

Thx, Andy

-- 
Twitter developer documentation and resources: http://dev.twitter.com/doc
API updates via Twitter: http://twitter.com/twitterapi
Issues/Enhancements Tracker: http://code.google.com/p/twitter-api/issues/list
Change your membership to this group: 
http://groups.google.com/group/twitter-development-talk

Reply via email to