Question

I am working on an Android 3.0 program that can connect to a SSL'd php webservice (outputs data in JSON, I can modify the server). The tablets that are connecting to this service have a corporate Microsoft ActiveSync (exchange 2010) account, and only that account (no google accounts,FB, etc). I would like to write a program that can use the credentials saved in the tablet's android.accounts.AccountManager to make secure requests on that PHP webservice. I tried following some google examples but I think the problem lies when I use the line: AccountManagerFuture<Bundle> data = am.getAuthToken(b, "JWT", options, this, ota, null); The application just hangs and I get no results. In fact, setting a breakpoint at any line in the OnTokenAcquired class, doesn't do anything. AKA OnTokenRequired never gets executed

Any advice or direction? I'm sure this could be helpful for getting corporate android client software

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class AcctestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AccountManager am = AccountManager.get(this);
        EditText et = (EditText) findViewById(R.id.editText1);
        OnTokenAcquired ota = new OnTokenAcquired(et);
        Account exchange = null;
        Bundle options = new Bundle();
        for(Account a : am.getAccounts()){
              if(a.type.equals("com.android.exchange") && a.name.endsWith("@domain.com"))
                     exchange = a;
        }
        AccountManagerFuture<Bundle> data = am.getAuthToken(exchange, "JWT", options, this, ota, null);
    }
}

import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class OnTokenAcquired implements AccountManagerCallback<Bundle> {
       private EditText et;
       public OnTokenAcquired(EditText et){
              this.et = et;
       }
       public void run(AccountManagerFuture<Bundle> result) {
              Bundle bundle;
              try {
                     Toast.makeText(null, "Start!", Toast.LENGTH_LONG).show();
                     bundle = result.getResult(1, TimeUnit.SECONDS);
                     String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);                         et.append("\nToken: " + token);
                     Toast.makeText(null, token, Toast.LENGTH_LONG).show();
              } catch (OperationCanceledException e) {
                     e.printStackTrace();
              } catch (AuthenticatorException e) {
                     e.printStackTrace();
              } catch (IOException e) {
                     e.printStackTrace();
              }            
       }
}
Was it helpful?

Solution

After some hours of research in the sources I've found this code:

public Bundle  getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions) throws NetworkErrorException {
     return null;
}

This means, the android exchange Authenticator doesn't provide the Tokenbased Authentication for Exchange-Server...

OTHER TIPS

You have to pass the proper token type for Exchange accounts. I don't know what it is offhand, but I doubt it would be 'test'. Research what the proper token type is (if supported) first. Otherwise your code looks OK. How to authenticate to Exchange using a token is another matter though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top