Android - 在 HttpsURLConnection 中传递 OAuth1
Android - pass OAuth1 in HttpsURLConnection
我是 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);
多亏了这个 .
更新
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.
我是 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);
多亏了这个
更新
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.