本文介绍了谷歌ClientLogin验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用认证<一个href=\"http://$c$c.google.com/googleapps/domain/email_settings/developers_guide_protocol.html#GA_email_settings_api_auth\">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();

不过,我得到错误= BadAuthentication 。我应该如何纠正我的code?

But I get Error=BadAuthentication. How I should correct my code?

推荐答案

您应该设置适当的应用程序/ x-WWW的形式urlen $ C $光盘内容类型并使用的OutputStream 写的POST主体。

You should set the proper application/x-www-form-urlencoded Content-type and use the OutputStream to write the POST body.

//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();
}

请参阅例子来处理结果,以获得 AUTH 标记。

See this example to handle the result to get the auth token.

这篇关于谷歌ClientLogin验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:08