Google OAuth access token expiration in MVC app?

后端 未结 1 839
Happy的楠姐
Happy的楠姐 2020-12-09 13:19

I wrote an MVC app using Google Oauth2 as instructed here: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications

I have an issue w

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 13:43

    Oh, I finally got it :) For those who interested - refresh token is only issued once, when you get that Consent screen, where you have to click Yes.

    So, in order to get refresh token, go to your account setting, Account Permissions: https://security.google.com/settings/security/permissions

    and revoke access for the project you configured in Google Developers Console: https://console.developers.google.com/project

    Now, put a breakpoint on the next line after you call AuthorizeAsync, restart your application in Debug mode, get that consent screen asking for permissions, click Accept.

    The app will return to VS and will stop on your break point.

    Now, record somewhere the result.Credential.Token.RefreshToken value, it's an encrypted string.

    I placed my in web.config appsetting for simplicity.

    Now, I just assign that value back to result.Credential.Token.RefreshToken = refreshToken;

    and every time, when access token expires, it will automatically refresh it.

    enter image description here

    Like here when I call GmailService request.Execute(...) passing the credential object that contains the token, the token will be refreshed.

    enter image description here

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