1

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Nadyn
  • 11
  • 2
  • Where are you specifying the userEmail? Also, can you reformat your code so we don't need to scroll sideways as much, please? – Jon Skeet Sep 04 '15 at 12:06
  • FileDataStore stores your authentication associated with what ever Me.UserEmail you are sending. You will need to either change that out or create your own implementation of Idatastore to move around your tokens. – Linda Lawton - DaImTo Sep 04 '15 at 12:32
  • Sorry, I've edited the code so it's clearer. The userEmail is passed at the declaration of credentials. But I don't think it matters what I put there – Nadyn Sep 04 '15 at 12:32
  • @DaImTo But that's not my problem I think. My problem is that the first time I run the program, it all works fine(as long I'm logged with the account I want to upload the file to), but then the second time if I change the account I'm logged with in the browser and try to upload a file again, it does upload the file, yes, but to the account I'm logged with in the browser at that time. – Nadyn Sep 04 '15 at 13:23

1 Answers1

1

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 ?)

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115