0

I have been trying to get Google Drive API working with .Net Core Identity (which has google Oauth2 built in)

I have tried following this tutorial. But it is not for .Net Core. How would I use .Net Core Identity to be able to access google drive after they log in?

Kuzon
  • 782
  • 5
  • 20
  • 49

1 Answers1

0

You'll need your Google console app to have the Drive API enabled then you'll need to add the correct scope to your Identity configuration in startup.cs. This will ensure that when your user logs in they get the correct scope assigned to their login token(s).

An example, if you want to read files and/or file META from Drive you might have: https://www.googleapis.com/auth/drive.readonly

See here for scopes: https://developers.google.com/drive/api/v2/about-auth

Here is a sample of how that might look:

        services.AddAuthentication().AddGoogle(googleOptions =>
        {
            googleOptions.ClientId = "YOUR_CLIENT_ID";
            googleOptions.ClientSecret = "YOUR_CLIENT_SECRET";
            googleOptions.Scope.Add("https://www.googleapis.com/auth/drive.readonly");
            googleOptions.SaveTokens = true;
            ...
        });

From here your user will have an AccessToken and RefreshToken returned when they log in. You can use this (along with their email address) to access their Google Drive.

I have a service which I use to make various requests to the API.

scgough
  • 5,099
  • 3
  • 30
  • 48
  • 1
    thanks for the info! this is really helping. Can you help me use those tokens to create a UserCredential so that I can access the API? I found this, https://stackoverflow.com/questions/38390197/how-to-create-a-instance-of-usercredential-if-i-already-have-the-value-of-access But I am unsure how to get the tokens, and what to do with the Datastore. Thanks, – Kuzon Jan 20 '20 at 23:33
  • an example on how to initialize a drive service instance using the logged in idenitity user (logged in with an identity Google external login) would be much aprreciated indeed :) – Kappacake Oct 06 '22 at 23:17