I am Developing an application which uses Login to Gmail.I gone through all tutorials,stackoverflow questions which is tagged under OAuth 2.0 and documents that
Consider using "localhost:port" as the redirect uri instead of urn:ietf:wg:oauth:2.0:oob& (for more details refer to https://developers.google.com/youtube/v3/guides/authentication#installed-apps) and trap this URL being loaded in the webview in the callback: public boolean shouldOverrideUrlLoading (WebView view, String url).
Below steps are require to login to Google.
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 to google.
NOTE: you will get Authentication full code from here Authentication code , put your gmail a/c and token where required. now you are able to loging using OAuth.
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);
6- in your AndroidManifest.xml add below uses permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
Thats all you require, now enjoy.
For an alternative way of accessing the Gmail API with an OAuth2 token, you may consider the answers from this post: OAuth and Java (connecting to GMail).