1

So I followed a few examples from http://www.daimto.com/, namely http://www.daimto.com/webmaster-tools-api-with-c/. After a long night I was able to get it to work. the way I did that is by simply removing

new FileDataStore("Daimto.GoogleWebMasters.Auth.Store")

changing the code to:

  UserCredential credential = GoogleWebAuthorizationBroker(new ClientSecrets 
{ ClientId = clientId, ClientSecret = clientSecret }                                                                                            
, scopes                                                                                     
, userName                                                                                 
, CancellationToken.None                                                                      
, null).Result; // <-- notice null here

So is the datastore really necessary? when I add it, my IIS is generating a new port number each refresh, making it impossible to authorize url redirects in google. btw, I tried the physical full path of the directory, but it results the same.

Shiran Dror
  • 1,472
  • 1
  • 23
  • 36

2 Answers2

1

If you don't include file datastore or any datastore for that matter by default the client library will use file datastore and create the file in %appData%

So technically speaking you don't need to use it.

What does FileDataStore do exactly?

Lets look at FileDataStore. When the following code authenticates. A folder called Drive.Auth.Store will be created in the %AppData% directory on the machine executing the code.

So we will have a new directory called %AppDatat%\Drive.Auth.Store . When I check my machine I find it here C:\Users\lindaHP\AppData\Roaming\Drive.Auth.Store

UserCredential credential;
using (var stream = new FileStream(clientSecretsJsonFilePath
                                   ,FileMode.Open
                                   ,FileAccess.Read))
      {   
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
      GoogleClientSecrets.Load(stream).Secrets,
      new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
      "LookIAmAUniqueUser",
       CancellationToken.None,
      new FileDataStore("Drive.Auth.Store")                               
      ).Result;
      }

Assuming the user clicks accept on the authentication request screen, a new file will be created in that directory with the following structure:

Google.Apis.Auth.OAuth2.Responses.TokenResponse-LookIAmAUniqueUser.TokenResponse-LookIAmAUniqueUser

Each user will have their own file you change a user by changing the “LookIAmAUniqueUser” value.

The file contains all the information you need to gain access to this users account.

alternate tutorial

I have an other tutorial for you. Google .net – FileDatastore demystified

Note from me

I have no idea how not using filedatastore would change the port appearing or not. I need to test this it could be a bug in the client library. or a feature I guess depending upon how you look at it.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • In GoogleApis.Auth.DotNet4/OAuth2/LocalServerCodeReceiver.cs, the port is assigned randomly in GetRandomUnusedPort() – Mike Meinz Jan 23 '16 at 12:01
  • Thank you DalmTo, I read what you suggested, and tried a custom DataStore. This did not change the random port problem. Furthermore, I discovered that when I publish the solution, it throws an error. All these issues were solved by avoid using GoogleWebAuthorizationBroker and going with the solution I linked in the answer I've added. – Shiran Dror Jan 23 '16 at 22:21
0

It seems this solution was just the wrong way, perhaps just obsolete. Finally, I rewrote everything base on this. Now it works find both on local and published, and with FileDataStore.

Community
  • 1
  • 1
Shiran Dror
  • 1,472
  • 1
  • 23
  • 36