HTTPS通信で苦戦中〜スケジュール管理ソフトをS!アプリで作ってみよう(その24)

オフィシャル作者登録も完了して、外部との通信が許可されるようになった。

早速、HTTPSの動作を試してみた。なんか、認証関係でエラーになるみたい。ググってみると、GoogleSSLの証明書は、携帯のアプリでは使えないっぽい。

弱ったなあ。

以下のコードでテストしてみたが、「javax.microedition.pki.CertificateException」のExceptionが発生してしまう。


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.HttpsConnection;

/**
* Google Calenderと連携するためのクラス
*/
public class GCal {
// 現在はテストコード
static String getAuthCode(String eMail, String passwd) {
String authCode = null;
HttpsConnection connection = null;
OutputStream oStream = null;
InputStream iStream = null;

try {
connection = (HttpsConnection) Connector.open("https://www.google.com/accounts/ClientLogin");
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

oStream = connection.openOutputStream();
oStream.write(("Email=" + eMail + "&Passwd=" + passwd
+ "&service=cl&source=com.ettem-Scheduler-0.7").getBytes());
oStream.write("\r\n\r\n".getBytes());
oStream.flush();

iStream = connection.openInputStream();
int len = (int) connection.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = iStream.read(data, bytesread, len - bytesread);
bytesread += actual;
}
authCode = new String(data);
authCode = authCode.substring(authCode.indexOf("Auth=") + "Auth=".length());
}
} catch (IOException e) {
authCode = e.toString();
} catch (Exception e) {
authCode = e.toString();
}

authCode = authCode.substring(authCode.indexOf("Auth=") + "Auth=".length());

return authCode;
}
}

上記のクラスは、以下のフォームから呼び出している。


package com.ettem.scheduler;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

public class GCalAuthForm extends Form implements CommandListener, ItemStateListener {
private Display display;
private Displayable callerDisplayable;

private TextField account;
private final int MAIL_ADDR_LEN = 64; // メールアドレスの長さ(TODO:RFCを調べる)
private TextField passwd;
private final int PASSWD_LEN = 64; // パスワードの長さ(TODO:Googleのアカウントを調べる)

private GCalAccount gCalAccount;

private Command okCommand;

public GCalAuthForm(Display display, Displayable displayable, GCalAccount gCalAccount) {
super("GCal Account");
this.display = display;
this.callerDisplayable = displayable;
this.gCalAccount = gCalAccount;

okCommand = new Command("OK", Command.OK, 1);
addCommand(okCommand);
setCommandListener(this);
setItemStateListener(this);

this.account = new TextField("Mail", "", MAIL_ADDR_LEN, TextField.ANY);
append(this.account);

this.passwd = new TextField("Password", "", PASSWD_LEN, TextField.PASSWORD);
append(this.passwd);
}

public void commandAction(Command command, Displayable displayable) {
// OKボタンが押されたら、呼び出しに戻る
if (command == okCommand) {

// テストコード
Event authEvent = Events.getNewEvent();
authEvent.what = GCal.getAuthCode(gCalAccount.account, gCalAccount.passwd);
Events.setEvent(authEvent);

display.setCurrent(callerDisplayable);
}
}

public void itemStateChanged(Item changedItem) {
if (changedItem == account) {
gCalAccount.account = account.getString();
} else if (changedItem == passwd) {
gCalAccount.passwd = passwd.getString();
}
}

}