简体   繁体   中英

getting account name in appengine connected android project

I have an android application connected to an appengine backend through endpoints. I would like the application to know the user's google account name and I wondered if there was an easy way, without going through all the authentication procedure described here
I don't really care about the security stuff at the moment, I would just like, for example, to be able to say "hello" to the user by using his name, or to store in the datastore a list of usernames that used the application. And I don't want users to be forced to enter a password to use my app.
I tried to use the code

GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(this, "appname.appengine.com");
String try= credential.getSelectedAccountName();

inside OnCreate() but it gives an null result. I also tried to put

UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();

inside the endpoint, but it doesn't work either.
Any help? Thanks in advance!

You can use the account manager to get the users' account w/o actually authenticating the accounts, eg:

// Requires android.permission.GET_ACCOUNTS
AccountManager manager = AccountManager.get(ctx);
Account[] accounts = null;
try {
    accounts = manager.getAccounts();
} catch (SecurityException e) {
    Log.i(CTAG, "Unable to read due to SE");
}
if (null != accounts)
    for (Account account : accounts)
        Log.v(CTAG, "New account =" + account.name + ", " + account.type );

This should satisfy your requirement to store a list of the users who used your app.

However, this does not give you the actual name of the user for greeting them. To get the users' name you need to use the profile API that was added with ICS (and which requires READ_PROFILE permission). You can get their name here:

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM