在https://accounts.google.com/o/oauth2/token请求令牌时,在谷歌上搜索“错误请求”的可能原因后,我决定问为什么无法获取此代码只不过是来自服务器的“错误请求”响应......
String url = "https://accounts.google.com/o/oauth2/token";
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setChunkedStreamingMode(0);
con.setRequestMethod("POST");
con.setRequestProperty("Host", "accounts.google.com");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("code", authCode);
con.setRequestProperty("client_id",
"[CLIENT_ID]");
con.setRequestProperty("client_secret", "[CLIENT_SECRET");
con.setRequestProperty("redirect_uri",
"http://localhost:8080/login");
con.setRequestProperty("grant_type", "authorization_code");
// Send post request
con.setDoOutput(true);
我确实必须设置con.setChunkedStreamingMode(0)
,因为服务器返回了与内容长度相关的错误。
有任何想法吗?是否有必要将有效载荷放在一行中?如何?