質問

Access tokens periodically expire and, when that happens, need to be refreshed. When an access token expires or at any other time, your application may be able to use a refresh token to obtain a new, valid access token. Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process.

I take this information from https://developers.google.com/youtube/v3/guides/authentication

How can I be aware that access_token expired ?

役に立ちましたか?

解決

One very common approach is to catch HTTP Errors for all API requests you make. If you run into a 401 error it means the access token is not valid any more and you can try to refresh it and repeat the original request. If the token refresh fails it most likely means that the user has revoked access for your app.

In pseudo code:

result = makeApiRequest();
if (result.status == 401) {
    refreshToken();
    result = makeApiRequest();
}

Some of the Google APIs client libraries (for example the one for Python) will take care of that for you automatically, but unfortunately not all of them.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top