John,

You may want to watch this:

http://www.youtube.com/watch?v=M7SxNNC429U

Google I/O 2011: Android + App Engine: A Developer's Dream Combination

The video covers this plugin for Eclipse:

http://code.google.com/eclipse/

... which makes it very easy to create Andorid apps that talk to a GAE backend, complete with device-to-GAE authentication and C2DM.

-- Kostya

08.01.2012 18:43, John Goche ?????:

On Sun, Jan 8, 2012 at 3:20 PM, Nikolay Elenkov <[email protected] <mailto:[email protected]>> wrote:

    On Sun, Jan 8, 2012 at 9:24 PM, John Goche
    <[email protected] <mailto:[email protected]>>
    wrote:
    >
    > Hello,
    >
    > I am still trying to authenticate to app engine using  google
    accounts
    > and would like to figure out the details of how such
    authenticator works.
    > How do I detect in my servlet that the user has authenticated with
    > google accounts?

    Read the App Engine documentation first. Then start searching for
    random examples.


Hi, the relevant App Engine documentation on the server side seems to be this one for the Users service (nothing specific to android on the client side here):

http://code.google.com/appengine/docs/java/gettingstarted/usingusers.html

As I mentioned there is this site also from google:

http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager

I tried running the code with some modifications as follows but I get the following error message when I try to run the code on a device (on the emulator it works
but there are no accounts):

[2012-01-08 15:25:26 - ComNet] Starting activity com.foobar.hello.StockActivity on device 80A358189040054719 [2012-01-08 15:25:27 - ComNet] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.foobar.hello/.StockActivity } [2012-01-08 15:25:27 - ComNet] ActivityManager: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.foobar.hello/.StockActivity } from null (pid=9825, uid=2000) requires null

Not sure what I need to do to get rid of it...

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    gotAccount(false);
  }

  static final int DIALOG_ACCOUNTS = 0;

  @Override
  protected Dialog onCreateDialog(int id) {
    switch (id) {
      case DIALOG_ACCOUNTS:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select a Google account");
        final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
        final int size = accounts.length;
        String[] names = new String[size];
        for (int i = 0; i < size; i++) {
          names[i] = accounts[i].name;
        }
        builder.setItems(names, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            gotAccount(manager, accounts[which]);
          }
        });
        return builder.create();
    }
    return null;
  }

  static String PREF = "pref";

  private void gotAccount(boolean tokenExpired) {
    SharedPreferences settings = getSharedPreferences(PREF, 0);
    String accountName = settings.getString("accountName", null);
    if (accountName != null) {
      AccountManager manager = AccountManager.get(this);
      Account[] accounts = manager.getAccountsByType("com.google");
      int size = accounts.length;
      for (int i = 0; i < size; i++) {
        Account account = accounts[i];
        if (accountName.equals(account.name <http://account.name>)) {
          if (tokenExpired) {
            manager.invalidateAuthToken("com.google", this.authToken);
          }
          gotAccount(manager, account);
          return;
        }
      }
    }
    showDialog(DIALOG_ACCOUNTS);
  }

  private String AUTH_TOKEN_TYPE = "ah";

private void gotAccount(final AccountManager manager, final Account account) {
    SharedPreferences settings = getSharedPreferences(PREF, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("accountName", account.name <http://account.name>);
    editor.commit();
    new Thread() {

      @Override
      public void run() {
        try {
          final Bundle bundle =
manager.getAuthToken(account, AUTH_TOKEN_TYPE, true, null, null)
                  .getResult();
          runOnUiThread(new Runnable() {

            public void run() {
              try {
                if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                  Intent intent =
                      bundle.getParcelable(AccountManager.KEY_INTENT);
                  int flags = intent.getFlags();
                  flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                  intent.setFlags(flags);
                  startActivityForResult(intent, REQUEST_AUTHENTICATE);
} else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                  authenticatedClientLogin(
                      bundle.getString(AccountManager.KEY_AUTHTOKEN));
                }
              } catch (Exception e) {
                handleException(e);
              }
            }
          });
        } catch (Exception e) {
          handleException(e);
        }
      }
    }.start();
  }

  static final int REQUEST_AUTHENTICATE = 0;

  @Override
  protected void onActivityResult(
      int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
      case REQUEST_AUTHENTICATE:
        if (resultCode == RESULT_OK) {
          gotAccount(false);
        } else {
          showDialog(DIALOG_ACCOUNTS);
        }
        break;
    }
  }

  private void authenticatedClientLogin(String authToken) {
    this.authToken = authToken;
//((GoogleHeaders) transport.defaultHeaders).setGoogleLogin(authToken);
    authenticated();
  }

  void handleException(Exception e) {

    e.printStackTrace();

  }

  void authenticated() {

  }

  private String authToken;


    > Here is an example that uses "ah" as the string parameter to
    getAuthToken.
    > Is this what I use?
    >

    Yes, "ah" is for App Engine. If you are not sure, just try it out.
    If it works, you know it works :)


Thank you. I wonder what "ah" stands for.


    > Any help sincerely appreciated, I'm really stuck on this one.

    Here's a small library and an associated blog post by none other
    than Google's Tim Bray. If you can't make that work, go back a
    few steps and read the docs a couple of more times. If that doesn't
    make sense, go a few steps back and read about HTTP and OAuth.

    http://www.tbray.org/ongoing/When/201x/2011/09/29/AERC

    http://code.google.com/p/aerc/


Thanks! I am going to try this one! If I run into any trouble I'll let you know.

Best Regards,

John Goche

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to