我正在尝试从JavaFX桌面应用访问Drive Api。在我收到初始身份验证(又名code
)之后,我想让令牌能够使用api。配置在开发控制台中设置为本机应用程序。
当我尝试获取令牌时,我得到各种http错误(400,401,404,405,406,411)。我想有点编码和/或我将请求发送到服务器的方式有问题。 代码如下:
public void sendOAuth2TokenRequest(String initialOAuth2) {
final String charset = "UTF-8";
try {
String url = "https://www.googleapis.com/oauth2/v3/token";
HttpsURLConnection con = (HttpsURLConnection) new URL(url).openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Charset", charset);
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
OutputStream output = con.getOutputStream();
output.write(getEncodedUrlParms().getBytes(charset));
output.close();
BufferedReader responseReader =
new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = responseReader.readLine()) != null) {
response.append(inputLine);
}
Log.i(TAG, "OAuth2Request " + inputLine);
responseReader.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "OAuth2Request Error: " + e.getMessage());
}
}
private String getEncodedUrlParms() throws UnsupportedEncodingException {
return "code=" + URLEncoder.encode(getInitialCode(), "UTF-8") +
"&client_id=" + URLEncoder.encode(DriveHelper.CLIENT_ID, "UTF-8") +
"&client_secret=" + URLEncoder.encode(DriveHelper.CLIENT_SECRET, "UTF-8") +
"&redirect_uri=" + URLEncoder.encode("urn:ietf:wg:oauth:2.0:oob", "UTF-8") +
"&grant_type=" + URLEncoder.encode("authorization_code", "UTF-8");
}
请让我知道here:
的意思您从开发者控制台获取的客户端密码(对于注册为Android,iOS或Chrome应用程序的客户端可选)。
本机应用中是否需要client_secret
(必填)?