1

I am new to OAuth concept, I am trying to achieve this

I need to hit a Url with consumer key, consumer secret, user token and user secret using HttpsURLConnection

Here is my code snippet:

String endpointURL = "some_dummy_url";
URL url = new URL(endpointURL);
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestProperty("Content-Type", "application-json");
connection.setDoInput(true);
connection.setDoOutput(true);

String consumerKey = getConsumerKey();
String consumerSecret = getConsumerSecret();
String usertoken  = getUserToken();
String userSecret = getUserTokenSecret();

connection.setRequestProperty("Authorization", //pass the tokens);

connection.connect();

1 Answers1

1
String consumerKey = getConsumerKey();
String consumerSecret = getConsumerSecret();

String baseAuthStr = consumerKey + ":" + consumerSecret;
connection.addRequestProperty("Authorization", "Basic " + baseAuthStr);

Thanks to this answer.

Update

URL url = new URL("https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + your_api_key);
URLConnection conn = (HttpURLConnection) url.openConnection();

conn.addRequestProperty("client_id", usertoken);
conn.addRequestProperty("client_secret", userSecret);


String token = consumerKey + ":" + consumerSecret;
Base64.Encoder encoder = Base64.getEncoder();
String encodedString = encoder.encodeToString(auth.getBytes(StandardCharsets.UTF_8) );
//conn.setRequestProperty("Authorization", "OAuth " + token);
connection.addRequestProperty("Authorization", "Basic " + token);

Reference doc.

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73