public OAuth2Token getToken(String code) throws IOException {
HttpsURLConnection connection = postTokenEndpoint(getAccessTokenPostData(code));
final int responseCode = connection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
OAuth2Token token = parseResponse(connection.getInputStream());
String username = getUsernameFromContacts(token);
Log.d(TAG, "got token " + token.getTokenForLogging()+ ", username="+username);
return new OAuth2Token(token.accessToken, token.tokenType, token.refreshToken, token.expiresIn, username);
} else {
Log.e(TAG, "error: " + responseCode);
throw new IOException("Invalid response from server:" + responseCode);
}
}