6

私はClientLoginGoogleの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(); 
} 

authトークンを取得するには、結果を処理するためにthis例を参照してください。

+0

+1ありがとうございます。残念ながら、私は明日のみそれをチェックする可能性があります。 –