.netgoogle-drive-api

Google Drive API OAuth authentication for server-side app


I'm trying to upload some CSV files up to a google drive via a .Net web app. My first attempt doing this using a service account ended up a failure. My second attempt using OAuth for authentication has so far been successful (when running locally), but I fear that this won't work once deployed to a web server.

I'm using the following code to authenticate (VB.Net):

Dim userCreds As UserCredential

Using fs As New FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)
    userCreds = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.FromStream(fs).Secrets, myScopes, Environment.UserName, CancellationToken.None).Result()
End Using

The documentation for GoogleWebAuthorizationBroker states that this class "is only suitable for client-side use, as it starts a local browser that requires user interaction."

What would be the suitable way to authenticate via OAuth in a .Net server-side app?


Solution

  • Simple Answer

    Use GoogleWebAuthorizationBroker for desktop.

    For server-side web apps, use GoogleAuthorizationCodeFlow