英文:
Go from OkHttp to HttpURLConnection
问题
private void getAccessToken(){
try {
URL url = new URL("https://www.googleapis.com/oauth2/v4/token");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
String requestBody = "grant_type=authorization_code" +
"&client_id=1568xxxxxxxxxxxxxxxxxjro.apps.googleusercontent.com" +
"&client_secret=AMe0xxxxxxxxxxxx" +
"&redirect_uri=" +
"&code=" + serverCode;
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody.getBytes());
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject jsonObject = new JSONObject(response.toString());
token = jsonObject.optString("access_token");
tokenExpired = SystemClock.elapsedRealtime() + jsonObject.optLong("expires_in") * 1000;
createGooglePhotosClient();
} else {
Log.i("severcode", "failure");
}
} catch (Exception e) {
e.printStackTrace();
}
}
英文:
For reasons of library compatibility issues I would like to use HttpURLConnection to call requests on an API.
Here is the code I use with OkHttp to get a token access:
private void getAccessToken(){
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormEncodingBuilder().add("grant_type", "authorization_code")
.add("client_id", "1568xxxxxxxxxxxxxxxxxjro.apps.googleusercontent.com")
.add("client_secret", "AMe0xxxxxxxxxxxx")
.add("redirect_uri", "")
.add("code", serverCode)
.build();
Request request = new Request.Builder()
.url("https://www.googleapis.com/oauth2/v4/token")
.post(requestBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.i("severcode","failure");
}
@Override
public void onResponse(Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
token = jsonObject.optString("access_token");
tokenExpired = SystemClock.elapsedRealtime() + jsonObject.optLong("expires_in") * 1000;
Log.i("severcode",String.valueOf(token));
createGooglePhotosClient();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
So I would like to know how to get the equivalent of requestbody to pass it in setRequestProperty ()?
Thanks for your help
答案1
得分: 1
请求体不是一个请求属性(头部),而是请求的主体,在没有OkHttp或其他支持库的情况下,您必须自己格式化它,对需要编码的特殊字符进行编码等。
String requestBody = "grant_type=authorization_code&client_id=1568xxxxxxxxxxxxxxxxxjro.apps.googleusercontent.com&"
+ "client_secret=AMe0xxxxxxxxxxxx&redirect_uri=&code=" + serverCode + "\n\n";
byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
一旦您有了请求主体,您就可以将其写入连接的输出流。例如:
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
out = connection.getOutputStream();
out.write(requestBodyBytes);
out.flush();
英文:
The request body is not a request property (header), it's the body of the request, and without OkHttp or other supporting libraries you have to format it yourself, encode any special characters that need to be encoded etc.
String requestBody = "grant_type=authorization_code&client_id=1568xxxxxxxxxxxxxxxxxjro.apps.googleusercontent.com&"
+ "client_secret=AMe0xxxxxxxxxxxx&redirect_uri=&code=" + serverCode + "\n\n";
byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
Once you have the request body, you write it to the connection's output stream. For example:
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
out = connection.getOutputStream();
out.write(requestBodyBytes)
out.flush();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论