我是OAuth概念的新手,我正在努力实现这一目标
我需要使用HttpsURLConnection使用用户密钥,用户密钥,用户令牌和用户密钥打一个Url
这是我的代码段:
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();
最佳答案
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。
关于android - Android-在HttpsURLConnection中传递OAuth1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51501516/