Sta*_*lin 6 java authentication google-app-engine google-api google-authentication
我尝试使用ClientLogin进行身份验证
URL url = new URL("https://www.google.com/accounts/ClientLogin");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Email", "testonly%2Ein%2E2011%40gmail%2Ecom");
connection.setRequestProperty("Passwd", "mypass");
connection.setRequestProperty("accountType", "HOSTED");
connection.setRequestProperty("service", "apps");
connection.connect();
Run Code Online (Sandbox Code Playgroud)
但我明白了Error=BadAuthentication.我该如何纠正我的代码?
您应该设置正确的application/x-www-form-urlencoded Content-type并使用OutputStream写入POST正文.
//Open the Connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Form the POST parameters
StringBuilder content = new StringBuilder();
content.append("Email=").append(URLEncoder.encode(youremail, "UTF-8"));
content.append("&Passwd=").append(URLEncoder.encode(yourpassword, "UTF-8"));
content.append("&service=").append(URLEncoder.encode(yourapp, "UTF-8"));
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(content.toString().getBytes("UTF-8"));
outputStream.close();
// Retrieve the output
int responseCode = urlConnection.getResponseCode();
InputStream inputStream;
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}
Run Code Online (Sandbox Code Playgroud)
请参阅此示例以处理获取auth令牌的结果.
| 归档时间: |
|
| 查看次数: |
3668 次 |
| 最近记录: |