[英]getting account name in appengine connected android project
I have an android application connected to an appengine backend through endpoints. 我有一个通过端点连接到appengine后端的android应用程序。 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
我希望应用程序知道用户的Google帐户名,并且想知道是否有一种简单的方法,而无需执行此处描述的所有身份验证过程
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. 在OnCreate()中,但结果为空。 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).
要获取用户名,您需要使用随ICS一起添加的配置文件API(并且需要READ_PROFILE权限)。 You can get their name here:
您可以在这里获得他们的名字:
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.StructuredName.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.