PHP:
public class SwedbankManager {
private static final String TAG = "SwedbankManager";
private static final String LOGIN_URL = "https://gfs.nb.se/bin2/gfskod";
private static final String ACCOUNTS_URL = "https://gfs.nb.se/bin2/gfskod?OBJECT=KF00T";
private static final String CREDITS_URL = "https://gfs.nb.se/bin2/gfskod?OBJECT=CC00T";
private static final String TOKEN_PARAM = "_csrf_token";
private static final String USER_PARAM = "kundnr";
private static final String PASS_PARAM = "pinkod";
private static final String OBJECT_PARAM = "OBJECT";
private static final String CHECKCODE_PARAM = "CHECKCODE";
private static final String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 1.5; en-se; HTC Hero Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1";
private static final String ACCOUNTS_REGEX =
"KF00T', '(\\d+)', '([\\w\\s\\d]+)'[^\\n]*\\s*<td class=\"last\" align=\"right\" nowrap>([0-9\\.\\,]+)";
private static final String CREDITS_REGEX =
"Kontoutdrag'; return true;\" onMouseOut=\"status='';\">([\\w\\s\\d]+)<[^\\n]*\\s*<td align=\"right\" class=\"last\" nowrap>([0-9\\.\\,]+)";
private String username;
private String password;
public SwedbankManager(String username, String password) {
this.username = username;
this.password = password;
}
public Account getAccount(int id) throws SwedbankException {
Hashtable<Integer, Account> accounts = getAccounts();
return accounts.get(Integer.valueOf(id));
}
public Hashtable<Integer, Account> getAccounts() throws SwedbankException {
Log.d(TAG, "-> getAccounts()");
HttpClient httpClient = new DefaultHttpClient();
//TODO: grab internal agent
//httpClient.getParams().setParameter(HttpProtocolParams.USER_AGENT, USER_AGENT);
Hashtable<Integer, Account> accounts = new Hashtable<Integer, Account>();
try {
// First get token
Log.d(TAG, "getting token...");
//String res = HTTPHelper.get(httpClient, LOGIN_URL);
//Pattern pattern = Pattern.compile(TOKEN_REGEX);
//Matcher matcher = pattern.matcher(res);
/*if (!matcher.find()) {
Log.e(TAG, "No token match.");
throw new SwedbankException("No token match.");
}
String token = matcher.group(1);
Log.d(TAG, "token= " + token);*/
// Then do login
List<NameValuePair> parameters = new ArrayList<NameValuePair>(3);
//parameters.add(new BasicNameValuePair(TOKEN_PARAM, token));
parameters.add(new BasicNameValuePair(USER_PARAM, username));
parameters.add(new BasicNameValuePair(PASS_PARAM, password));
parameters.add(new BasicNameValuePair(OBJECT_PARAM, "TT00"));
parameters.add(new BasicNameValuePair(CHECKCODE_PARAM, "124535648456899"));
Log.d(TAG, "logging in...");
String res = HTTPHelper.post(httpClient, LOGIN_URL, parameters);
//TODO: use regexp to grab swedbank err text
if (res.contains("Tekniskt fel")) {
//login failed.. bail
throw new SwedbankException("auth fail");
}
// Now we should be logged in with a cookie set, let's get accounts info
Log.d(TAG, "getting account info...");
res = HTTPHelper.get(httpClient, ACCOUNTS_URL);
Log.d(TAG, "accounts html dump:");
Log.d(TAG, res);
Pattern pattern = Pattern.compile(ACCOUNTS_REGEX);
Matcher matcher = pattern.matcher(res);
while (matcher.find()) {
//int groupCount = matcher.groupCount();
//if (groupCount < 4) {
// throw new SwedbankException("Pattern match issue: groupCount < 4");
//}
//for (int i = 1; i <= groupCount; i++) {
// Log.d(TAG, i + ":" + matcher.group(i));
//}
int id = Integer.parseInt(matcher.group(1));
int ordinal = Integer.parseInt(matcher.group(1));
String name = matcher.group(2);
long balance = Long.parseLong(matcher.group(3).replaceAll("\\,|\\.", ""))/100;
accounts.put(Integer.valueOf(id), new Account(id, ordinal, name, balance));
}
// Now we should be logged in with a cookie set, let's get accounts info
Log.d(TAG, "getting account info...");
res = HTTPHelper.get(httpClient, CREDITS_URL);
Log.d(TAG, "accounts html dump:");
Log.d(TAG, res);
pattern = Pattern.compile(CREDITS_REGEX);
matcher = pattern.matcher(res);
while (matcher.find()) {
//int groupCount = matcher.groupCount();
//if (groupCount < 4) {
// throw new SwedbankException("Pattern match issue: groupCount < 4");
//}
//for (int i = 1; i <= groupCount; i++) {
// Log.d(TAG, i + ":" + matcher.group(i));
//}
int id = 100;
int ordinal = id;
String name = matcher.group(1);
long balance = Long.parseLong(matcher.group(2).replaceAll("\\,|\\.", ""))/100;
accounts.put(Integer.valueOf(id), new Account(id, ordinal, name, balance));
}
//accounts.put(Integer.valueOf("5"), new Account(5, 5, "test", 2323));
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
throw new SwedbankException(e.getMessage(), e);
} catch (HTTPException e) {
Log.e(TAG, e.getMessage(), e);
throw new SwedbankException(e.getMessage(), e);
}
Log.d(TAG, "<- getAccounts()");
return accounts;
}
}
Använde krias källkod. Endast testat litegrann men det fungerar iaf.