0
votes

What we have to accomplish:

  • user open a web page where he/she signs in with Google login
  • access_token and refresh_token are send to our backend server
  • backend server has to read xlsx files stored on google drive (backend server makes request to Google Sheets API v4)

I used this plugin https://www.npmjs.com/package/angularx-social-login for web application using Angular 8.

Looking at the Google client api libraries for .NET - https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth there doesn't seem to be any support for setting access_token or refresh_token. Only client_id and client_secret.

My other approach was then to use REST API and it works:

static void Test()
{
  string baseUrl = "https://www.googleapis.com/drive/v3/files";
  string token = "ya29.ImC1B_sds9zLKAlruaMvkWqcw6vA_0oXRk4HOjLP4Du3S5TzBCg";             
  httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  string response = client.GetStringAsync(baseUrl + "/?q=mimeType='application/vnd.google-apps.spreadsheet'").Result;
}

Is there any way that I can use Google Client .NET library to refresh access_token or do I have to use REST API for this?

1

1 Answers

0
votes

Your back end should just forward the access token to the Google Rest API as above - the back end should not need to use a client ID and secret when calling Google APIs, since authentication has already completed.

Refreshing a Google access token is described in this page - if you understand the HTTP message you'll be able to code it quite easily.

Usually the process works like this:

  • Client triggers a request to the Google API
  • Client checks for a 401 response after calling the API
  • 401s are handled by refreshing the token and retrying the API call with a new access token