[Android] using Google SDK for SSO Login

Source: Internet
Author: User
1. Technical Solutions
Solution 1: AccountPicker + GoogleAuthUtil. Use AccountPicker to get username and GoogleAuthUtil to get assess token. This method supports single sign-on. The interface is drawn in the SDK, which is convenient and simple. The disadvantage is that AccountPicker does not support versions earlier than 4.2.x. Solution 2: AccountManager + GoogleAuthUtil. Use AccountManager to obtain username. AccountManager API Level5, the advantage of this method is that the compatibility is good, the disadvantage is that you need to customize a dialog box to prompt the user to select an account that has been logged on to the device, or create an account. The following two methods are described respectively.
2. AccountPicker + GoogleAuthUtilAPI documentation https://developer.android.com/reference/com/google/android/gms/common/AccountPicker.html

Http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html


User-Permission <uses-permission android: name = "android. permission. GET_ACCOUNTS "/> <uses-permission android: name =" android. permission. NETWORK "/> <uses-permission android: name =" android. permission. USE_CREDENTIALS "/> <uses-permission android: name =" android. permission. INTERNET "/>
First, call the SDK's Single Sign-On page. Private static final int REQ_CODE_PICK_ACCOUNT = 3333; // The value can be customized. Intent intent = AccountPicker. newChooseAccountIntent (null, null, new String [] {"com. google "}, false, null, null) startActivityForResult (intent, REQ_CODE_PICK_ACCOUNT); then, get username. Protected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {case REQ_CODE_PICK_ACCOUNT: if (resultCode = RESULT_ OK) {// user name retrieved successfully String name = data. getStringExtra (AccountManager. KEY_ACCOUNT_NAME); // username} else {// failed to get user name} break ;......} finally, obtain the token. String token = GoogleAuthUtil. getToken (MainActivity. this, userName, "oau2:" + scope); // In the background thread, note the scope settings. The format is "oauty2: scope1 scope2 scope3", the https://developers.google.com/+/api/oauth uses "https://www.googleapis.com/auth/plus.login" and "https://www.googleapis.com/auth/userinfo.email", these two scopes are enough.
3. AccountManager + GoogleAuthUtil
API documentation http://developer.android.com/reference/android/accounts/AccountManager.html
User-Permission <uses-permission android: name = "android. permission. GET_ACCOUNTS "/> <uses-permission android: name =" android. permission. NETWORK "/> <uses-permission android: name =" android. permission. USE_CREDENTIALS "/> <uses-permission android: name =" android. permission. INTERNET "/> <uses-permission android: name =" android. permission. MANAGE_ACCOUNTS "/> <uses-permission android: name =" com. google. android. goo Gleapps. permission. GOOGLE_AUTH "/> <uses-permission android: name =" com. google. android. googleapps. permission. GOOGLE_AUTH.wise "/> <uses-permission android: name =" com. google. android. googleapps. permission. GOOGLE_AUTH.writely "/> get the Google account that is logged on to the device. AccountManager manager = AccountManager. get (this); Account [] accounts = manager. getAccountsByType ("com. google"); log on to another Google Account or create a Google Account and obtain the Account. Manager. addAccount ("com. google ", null, this, new AccountManagerCallback <Bundle> () {@ Override public void run (AccountManagerFuture <Bundle> future) {if (future. isDone () {try {Bundle bundle = future. getResult (); String name = bundle. getString (AccountManager. KEY_ACCOUNT_NAME);} catch (Exeception e) {e. printStackTrace () ;}}}, null); Generally, first obtain the account on the device. If there is only one account on the device, directly obtain the Token of the account; if the device does not have an account And directly use manager. add an account to the addAccount. After the account is successfully added, obtain the Token of the account. If there are multiple accounts on the device, a dialog box is displayed. You can select multiple accounts or create new accounts, based on the user's choice, make corresponding processing.
4. Verify that the obtained Token is valid.Https://www.googleapis.com/oauth2/v1/userinfo? Access_token = the obtained token


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.