0

Trying to authenticate the Google coordinate api. Tried the Service acount authentication usisng service account and posted stack flow with this question. Found this anwer and quite describes my question.

What the problem now is the library used is deprecated. Not able to execute the solution said.

var auth = new OAuth2Authenticator<WebServerClient> (provider, GetAuthorization); 

        // Create the service. 
        var service = new CoordinateService(new BaseClientService.Initializer()
                   {
                      Authenticator = auth
                   });

can anybody suggest a way to achieve the above code. I have installed the new version for Google APIs OAuth2 Client Library. But didn't found any similar code.

I am able to do the read the api using the below code snippet

using Google.Apis.Auth.OAuth2;
using Google.Apis.Coordinate.v1;
using Google.Apis.Coordinate.v1.Data;
using Google.Apis.Services;

                using (var stream = new FileStream(System.Web.HttpContext.Current.Server.MapPath(@"../client_secret.json"), FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                    new[] { CoordinateService.Scope.Coordinate },
                    "user", CancellationToken.None);
            }               
            // Create the service.
            var service = new CoordinateService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "test",
            });
            //builds with time last day 12 am 

            var locationReq = service.Location.List(teamId, workerMail, (ulong)DateTime.Today.AddDays(-1).ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds);
 var locationResult = locationReq.Execute();

but this method reuires a redirection for the first time. I can't do that in my scenario. so need an offline solution.

Community
  • 1
  • 1
Anish Karunakaran
  • 987
  • 3
  • 17
  • 39

2 Answers2

1

It looks like you are using a very old version of the library (pre GA).

I recommend you to download the latest Coordinate API, which is available in NuGet - https://www.nuget.org/packages/Google.Apis.Coordinate.v1/.
Then, follow the get started and the OAuth 2.0 pages. You should find all the right documentation there, if something is missing - let us know. Open an issue in our issue tracker.

peleyal
  • 3,472
  • 1
  • 14
  • 25
  • I have updated with my question with the latest code.. Please have a look – Anish Karunakaran Nov 12 '14 at 06:38
  • I don't see that you call the Execute method on the service.Location.List(...) object, why is that? The 2nd thing - it looks like you need a service account solution - take a look in this documentation - https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service_account – peleyal Nov 12 '14 at 14:07
  • Service accounts cannot be used with the Coordinate API. [this is because the Coordinate API requires authenticated API users to have a Coordinate license, but it is not possible to attach a Coordinate license to a service account] – Anish Karunakaran Nov 12 '14 at 16:20
  • My need is to get the code sample from the new version of GA for to run the solution said here http://stackoverflow.com/questions/17177262/google-coordinate-oath2-with-service-account – Anish Karunakaran Nov 12 '14 at 16:22
  • OK.. sorry :( so you will have to user authenticate once, and then reuse the refresh token to keep your access token refreshed and valid (the library will take care of that for you) – peleyal Nov 12 '14 at 16:49
  • But with the new library we didn't found any method which does the thing as in old library. Any code sample will be great. – Anish Karunakaran Nov 13 '14 at 05:13
  • I'm confused. 1- Did service account worked for you before? 2- Why can't you authorize the app once and from there the app will have the refresh token to keep a fresh access token? – peleyal Nov 13 '14 at 12:50
  • 1. Service account didn't work. 2. http://stackoverflow.com/questions/17177262/google-coordinate-oath2-with-service-account This solution tells how to achieve it. The Google library used is obsolete. But In new library we didn't found the way to achieve it. – Anish Karunakaran Nov 14 '14 at 08:59
1

Need to authenticate using a browser instance first and can reuse same "refresh token" for all other requests. We can provide a custom folder location to store the "auth token" and the library will use stored token from that folder. Modified source code pasted below:

using (var stream = new FileStream(@"client_secret.json", FileMode.Open, FileAccess.Read))

{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
    new[] { CoordinateService.Scope.Coordinate },
    "user", CancellationToken.None,new FileDataStore(folder));
}

MG

jineesh MG
  • 61
  • 8