OAuth2 and Google API: access token expiration time?

137,106

Solution 1

You shouldn't design your application based on specific lifetimes of access tokens. Just assume they are (very) short lived.

However, after a successful completion of the OAuth2 installed application flow, you will get back a refresh token. This refresh token never expires, and you can use it to exchange it for an access token as needed. Save the refresh tokens, and use them to get access tokens on-demand (which should then immediately be used to get access to user data).

EDIT: My comments above notwithstanding, there are two easy ways to get the access token expiration time:

  1. It is a parameter in the response (expires_in)when you exchange your refresh token (using /o/oauth2/token endpoint). More details.
  2. There is also an API that returns the remaining lifetime of the access_token:

    https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={accessToken}

    This will return a json array that will contain an expires_in parameter, which is the number of seconds left in the lifetime of the token.

Solution 2

The default expiry_date for google oauth2 access token is 1 hour. The expiry_date is in the Unix epoch time in milliseconds. If you want to read this in human readable format then you can simply check it here..Unix timestamp to human readable time

Share:
137,106
Martin V.
Author by

Martin V.

Java EE Developer

Updated on July 05, 2022

Comments

  • Martin V.
    Martin V. almost 2 years

    We have a standalone Java application (see "Installed application") which runs periodically and uses Google API (updates some information from customer databases/ldap/...).

    To access Google APIs we store username and password in configuration file, which is a security risk and customer does not like that. So we would like to use OAuth2 long-living access token instead.

    What`s default expiration time for Google OAuth2 access tokens ?

    As we will have only access token in application, app itself cannot refresh it when access token expires.

    Personally I think that OAuth2 implementation in this case will not bring any major benefit but let`s focus on main question - default expiration times.

  • Martin V.
    Martin V. over 11 years
    SO i need client secret only for first autorization and then later after i will get access and refresh token app can have access
  • vlatko
    vlatko over 11 years
    @martin85 Well, there are multiple steps involved. First obtain the authorization code, then exchange the authorization code for a refresh token (here's where you would use the client secret). Once you have the refresh token, you can exchange it for an access token. The web UI will be shown to the user only when you are obtaining the refresh token (you have to get user's approval to access their data). After that the flow does not need to involve the user.
  • Martin V.
    Martin V. over 11 years
    Is it possible to make autorization and approval step purely programatically ? I am not running any webapp - it`s more batch/long running daemon app.
  • vlatko
    vlatko over 11 years
    @martin85 yes, you could bypass the user UI with the OAuth2 Service Accounts flow, assuming your application has access to the API. See this question for an example w/ the Google Analytics API.
  • monstro
    monstro over 9 years
    vlatko, you don't necessary get a refresh token upon successful authorization. Look here, at my post (stackoverflow.com/questions/27678496/…), Google OAuth2 service doesn't issue a refresh token, at least this is what I see debugging the application.
  • Alex
    Alex over 9 years
    You have to set accessType to 'offline' to obtain a refresh token
  • sasha.sochka
    sasha.sochka over 8 years
    What do you do with OAuth mobile flow? You don't have a refresh_token in mobile apps auth, because you can't have a secret code (anybody could decompile the app and see it). What happens when the token gets expired?
  • Internial
    Internial over 6 years
    @vlatko what does very short lived mean?
  • z80crew
    z80crew almost 6 years
    @Internial Right now, Google access tokens have a TTL of 1 hour.
  • giaggi
    giaggi almost 3 years
    Hey all, question on this one. When we exchange a refresh token does the user need to login again with gmail?