accountmanager refresh token(offline access)

后端 未结 3 1356
梦毁少年i
梦毁少年i 2020-12-30 14:45

I use google login through account manager in my android app. I can get accesstoken which I send to server and server can create/login new user. Accesstoken is valid only 36

3条回答
  •  天命终不由人
    2020-12-30 15:17

    Google Authorization process through account manager:

    Email id can be got from

    AccountManager accountManager = AccountManager.get(getApplicationContext());
    Account[] accounts = accountManager.getAccountsByType("com.google");
    String emailID = accounts[0].name; // you can retrieve using google account chooser way also
    

    These lines should be run in separate token(not in UI thread).

    String scope = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com";
    String accessToken = GoogleAuthUtil.getToken(mContext, emailID, scope); 
    

    save the the accessToken and use for your api access.

    After one hour (i.e. 3600 seconds), we need to refresh the access token. But now google is not supporting access after one hour. We have to restart the application and use the following lines to get access token.

    String scope = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com";
    String accessToken = GoogleAuthUtil.getToken(mContext, emailID, scope);
    

    This background thread will always run in background in while loop

提交回复
热议问题