Using Oauth 2.0 login gmail in android
Below steps require to login gmail using Oauth2.0
1- Select an account from your device using below code
Below steps require to login gmail using Oauth2.0
1- Select an account from your device using below code
public static AccountManager accountManager;
accountManager = AccountManager.get(this);
Account[] accounts = accountManager.getAccountsByType("com.google");
2- Get a Token from selected account using below code
private void onAccountSelected(final Account account) {
accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this,
new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
useToken(account, token);
} catch (OperationCanceledException e) {
onAccessDenied();
} catch (Exception e) {
handleException(e);
}
}
}, null);
}
3-
now authenticate the token using user account and token you will be able to login.
Note: After sometime token get unauthorized so you need to invalidate your token.
4- for re login you have to invalidate your token using below code
accountManager.invalidateAuthToken("com.google", token);
5- After invalidate you have to get a new token using below code
String newToken = AccountManager.get(this).getAuthToken(new Account(account, "com.google"),
AUTH_TOKEN_TYPE, true, null, null).getResult().getString(AccountManager.KEY_AUTHTOKEN);
Thats all you need to do. Any suggestion will be appreciated.
No comments:
Post a Comment