set the (OAuth token) Authorization Header on SpringBoot

Viewed 473

I have to send a Get Request to Request the Token URL with these Headers:

Content-Type: application/x-www-form-urlencoded
Authorization:
        OAuth oauth_consumer_key="your_consumer_key",
        oauth_nonce="random_string_or_timestamp",
        oauth_signature="your_consumer_secret&",
        oauth_signature_method="PLAINTEXT",
        oauth_timestamp="current_timestamp",
        oauth_callback="your_callback"
User-Agent: some_user_agent

I've tried with this piece of code:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");

        conn.setRequestProperty("Authorization: OAuth oauth_consumer_key", "BaeUqWMTmCxjeJj9mkJr");
        conn.setRequestProperty("Authorization: oauth_nonce", "random_string_or_timestamp");
        conn.setRequestProperty("Authorization: oauth_signature", "ZWglyBtJasnJBqVndzyduYJggCduKeYks&");
        conn.setRequestProperty("Authorization: oauth_timestamp", String.valueOf(new Date()));
        conn.setRequestProperty("Authorization: oauth_callback", "http://localhost:8080");
        conn.setRequestProperty("User-Agent", "test");

        int statusCode = conn.getResponseCode();
        System.out.println("Response from WA Gateway: \n");
        System.out.println("Status Code: " + statusCode);
        BufferedReader br = new BufferedReader(new InputStreamReader(
                (statusCode == 200) ? conn.getInputStream() : conn.getErrorStream()
        ));
        String output;
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        conn.disconnect();

But I get this error:

Illegal character(s) in message header field: Authorization: OAuth oauth_consumer_key
2 Answers

The problem is that you're not forming your request correctly. Since it's a GET version of OAuth request, here's an example from RFC5849 on how to do this

GET /example/path?oauth_consumer_key=0685bd9184jfhq22& oauth_token=ad180jjd733klru7&oauth_signature_method=HM AC-SHA1&oauth_signature=wOJIO9A2W5mFwDgiDvZbTSMK%2FPY% 3D&oauth_timestamp=137131200&oauth_nonce=4572616e48616 d6d65724c61686176&oauth_version=1.0 HTTP/1.1

Sp, set an Authorization header in the setRequestProperty following the format above:

...
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String yourUrl = "http://yourwebsite.com";
String currentSeconds = 1618496867; // current seconds
String authorizationValue = "OAuth oauth_consumer_key=\"BaeUqWMTmCxjeJj9mkJr\",
    oauth_nonce=\"1618496867\",
    oauth_signature=\"ZWglyBtJasnJBqVndzyduYJggCduKeYks&\",
    oauth_signature_method=\"PLAINTEXT\",
    oauth_timestamp=\"1618496867\",
    oauth_callback=\"http://localhost:8080\""

conn.setRequestProperty("Authorization", authorizationValue);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
...

I tested a similar request using a Postman client for my OAuth authentication server and it worked perfectly.

Related