Android: How to get Refresh Token by Google Sign-In API?

前端 未结 2 851
萌比男神i
萌比男神i 2020-12-29 08:05

Currently, I am working on the application where user able to login with Google. As part of the login process, we need to send Google ACCESS TOKEN and REFRESH TOKEN to serve

2条回答
  •  误落风尘
    2020-12-29 08:47

    I think you need to try this code in AsyncTask like below.

    private class RetrieveTokenTask extends AsyncTask {
    
        @Override
        protected String doInBackground(String... params) {
            String accountName = params[0];
            String scopes = "oauth2:profile email";
            String token = null;
            try {
                token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } catch (UserRecoverableAuthException e) {
                startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
        //REQ_SIGN_IN_REQUIRED = 55664;
            } catch (GoogleAuthException e) {
                Log.e(TAG, e.getMessage());
            }
            return token;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("AccessToken",s);
        }
    }
    

    Then call AsyncTask like below to get Access Token:

    ...    
    new RetrieveTokenTask().execute(mAccountName);
    

    Check here. I hope it's help you.

提交回复
热议问题