Android-在HttpsURLConnection中传递OAuth1

时间:2018-07-24 14:44:28

标签: android oauth httpsurlconnection

我是OAuth概念的新手,我正在努力实现这一目标

我需要使用HttpsURLConnection敲打一个包含用户密钥,用户密钥,用户令牌和用户密钥的网址

这是我的代码段:

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 个答案:

答案 0 :(得分:1)

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

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

感谢 answer

更新

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);

参考 doc