6

나는 ClientLogin구글 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(); 

사용하여 인증을 시도하지만 Error=BadAuthentication를 얻을. 내 코드를 어떻게 수정해야합니까?

+0

문제가있는 경우 GAE 플랫폼에서 실행합니다. –

답변

5

올바른 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(); 
} 

this 예는 auth 토큰을 얻을 수있는 결과를 처리하기를 참조하십시오.

+0

+1 감사합니다. 불행히도, 나는 내일에만 그것을 확인할 가능성이있다. –