我正在尝试使用简单的Java程序与Google进行身份验证。我用我的凭据发布到正确的URL。我收到HTTP状态代码200的响应,但不包含我需要为用户检索订阅源的任何身份验证令牌。这是代码
private static String postData = "https://www.google.com/accounts/ClientLogin?Content-type=application/x-www-form-urlencoded&accountType=GOOGLE&Email=xxxxxxxx&Passwd=xxxxx";
public GoogleConnector(){
HttpClient client=new DefaultHttpClient();
HttpPost method=new HttpPost(postData);
try{
HttpResponse response=client.execute(method);
System.out.println(response.toString());
}
catch(Exception e){
}
答案 0 :(得分:1)
好的,你遇到的第一个问题是'Content-Type'需要是标题,而不是请求参数。其次,POST参数应该附加到请求主体,而不是请求URL。您的代码应如下所示:
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost("https://www.google.com/accounts/ClientLogin");
method.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(4);
postParams.add(new BasicNameValuePair("accountType", "GOOGLE"));
postParams.add(new BasicNameValuePair("Email", "xxxxxxx"));
postParams.add(new BasicNameValuePair("Passwd", "xxxxxx"));
postParams.add(new BasicNameValuePair("service", "cl"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParams);
method.setEntity(formEntity);
HttpResponse response=client.execute(method);
System.out.println(response.toString());