accountmanager refresh token(offline access)

后端 未结 3 1333
梦毁少年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:13

    This is now possible: https://developers.google.com/+/mobile/android/sign-in#server-side_access_for_your_app

    You request a one-time authorisation code, send it to your server and your server exchanges it for an access token and refresh token.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-30 15:19

    Currently you can't, which I'm sure is not the answer you're hoping for, sorry about that! If you have a web sign in, you can use the hybrid flow to get a refresh token on the server as well (see https://developers.google.com/+/web/signin/server-side-flow), but there's no way of retrieving a code as part of the Android or iOS flows.

    If this is something that you need for you use case, could you file a feature request on here: https://code.google.com/p/google-plus-platform/issues - we are actively looking at the number of stars on that to gauge demand for various features.

    0 讨论(0)
提交回复
热议问题