ChatGPT解决这个技术问题 Extra ChatGPT

OAuth2 and Google API: access token expiration time?

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.


v
vlatko

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:

It is a parameter in the response (expires_in)when you exchange your refresh token (using /o/oauth2/token endpoint). More details. 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.


SO i need client secret only for first autorization and then later after i will get access and refresh token app can have access
@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.
@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.
You have to set accessType to 'offline' to obtain a refresh token
@Internial Right now, Google access tokens have a TTL of 1 hour.
R
Rajkumar Bansal

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


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now