How to Login into Gmail using OAuth in Android Application?

后端 未结 3 2209
难免孤独
难免孤独 2020-12-18 15:37

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

3条回答
  •  醉酒成梦
    2020-12-18 16:27

    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() {
    public void run(AccountManagerFuture 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

    
    
    
    

    Thats all you require, now enjoy.

提交回复
热议问题