Nadyn
Nadyn

Reputation: 11

Google Drive: Uploading to a different account than the active one

I've done a little program in VB.NET that, basically, synchronizes a directory with Google Drive and then it obtains the sharing link and creates a shortcut file for that that uploaded file. All works fine and well. Now, my problem came when I started playing with different accounts instead of just the account I was running tests on. I observed that when I authenticate with this code:

    Dim clientS As ClientSecrets = New ClientSecrets()
    clientS.ClientId = "ClientID"
    clientS.ClientSecret = "MySecret"
    Dim scopes As IList(Of String) = New List(Of String)()

    scopes.Add(DriveService.Scope.Drive)
    scopes.Add(DriveService.Scope.DriveFile)
    Dim credentials As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(clientS, scopes, Me.UserEmail, System.Threading.CancellationToken.None, New FileDataStore(Me.TokensFolderPath)).Result

    Dim initializer As BaseClientService.Initializer = New BaseClientService.Initializer()
    initializer.HttpClientInitializer = credentials
    initializer.ApplicationName = "Drive API Test"

    Me.service = New DriveService(initializer)

The first time this will open a browser window and ask the users for permissions to view, edit, ... your files. And then it creates a token to avoid asking next time the application runs. The issue is that even if I'm specifying a userEmail on the code, it doesn't care. It will just open the browser and ask permissions with whatever account you have active at that precise moment, and so, the token created will belong to that account and not to the one I specified.

Am I doing something wrong or there is something missing? Or maybe the API is just supposed to work like that and it always uploads the files to the account you are curremtly logged in.

Upvotes: 1

Views: 190

Answers (1)

pinoyyid
pinoyyid

Reputation: 22306

This line Dim credentials As UserCredential = GoogleWebAuthorizationBroker... is saying, "please go ask the current logged in user for permission to access his drive, and store the result in credentials that I can then present to Drive".

If I understand your question correctly, what you want is "ignore the current logged in user. Please give me the credentials to a fixed account that I want to upload to, without having to go through any auth flow".

If I've got your question right, the answer is How do I authorise an app (web or installed) without user intervention? (canonical ?)

Upvotes: 1

Related Questions