1

I managed to get the access_token and refresh_token on front end and now its stored in a database, On the backend .netcore application I create googlecredential with

var cred=googleCredential.fromAccessToken(access_token);

but this one doesn't refresh on its own and I have no idea how to refresh this. I can send a post request to their rest api and refresh it but I need to know when "creds" has expired to send that refresh request. PS know that the tokens are in a database so using anything but .fromaccesstoken is not an option.

EDIT: Complete code can not be shared but here's how the application's flow is going. On a reactJS application I get the authorization code via react-google-login module

<GoogleLogin
                  
                  clientId="xxx"
                  buttonText="Login"
                  onSuccess={this.responseGoogle}
                  onFailure={this.responseGoogle}
                  cookiePolicy={"single_host_origin"}
                  scope="https://www.googleapis.com/auth/drive.readonly"
                  accessType="offline"
                  prompt="consent"
                  responseType="code"
                />

this returns an authorization code as the only response body, this response is then used with another endpoint to exchange for refresh and access token at front end, these tokens are sent to a backend endpoint which stores them in a db. Now the server side can create a googlecredential object via

this.googleCredentials=GoogleCredential.FromAccessToken(this.googleAccessToken);

Only problem being this doesnt handle the refreshing on its own so you have to somehow implement it.

Shahab Uddin
  • 101
  • 1
  • 11
  • 1
    I would love to see your code as to how you are storing it in the database. are you using the google .net client library? YOur probably going to need to feed it the refresh token and it should be able to request a new access token. I thought it was all stored in sessions so you wouldnt need to store it in a database. – Linda Lawton - DaImTo Feb 10 '21 at 14:09
  • I have added the information. PS the token is only short lasted and i wanted to use it later for accessing drive contacts etc – Shahab Uddin Feb 11 '21 at 10:10
  • Access tokens are only good for an hour you should be building the credentials from the refresh token. – Linda Lawton - DaImTo Feb 11 '21 at 12:06
  • In my case, I would create a google credential each time I want to use the API but before that, I would validate my access token if it's expired or not, if it's expired then I would refresh it using the refresh token before passing them to create Google creds type. – Synth Sloth Mar 06 '23 at 01:35

1 Answers1

0

Since I can't find another way to refresh when GoogleCredentials object is created via fromtoken(ACCESS_TOKEN) what I've done is sending a post request with the refresh token that I have to the rest endpoint at the backend and get a new access_token. Another problem faced was finding out when the access token was expired for which no solution could be found. But it returns a 401 exception at creating the drive folders via drive service in my case

var res = await this.driveService.Files.Create(folder).ExecuteAsync()

here res will raise an exception and I just put it in a try block and got the new access token via http request in catch and ran the statement again (hint: recursion)

Hope this helps anyone, but better proposed solutions will be welcomed. Please understand that this is going to be a backend solution reading refreshtoken from database.

Shahab Uddin
  • 101
  • 1
  • 11