Google Coordinate API身份验证离线

时间:2014-11-11 15:53:03

标签: c# .net google-api google-oauth google-api-dotnet-client

尝试验证Google坐标api。尝试了服务帐户身份验证usisng服务帐户,并使用this问题发布了堆栈流。找到了this anwer并且完全描述了我的问题。

现在的问题是库used已被弃用。无法执行解决方案说。

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

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

任何人都可以建议一种方法来实现上述代码。我已经为Google API OAuth2客户端库安装了新版本。但没有找到任何类似的代码。

我可以使用下面的代码片段来读取api

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();

但是这种方法第一次需要重定向。在我的场景中我无法做到这一点。所以需要离线解决方案。

2 个答案:

答案 0 :(得分:1)

看起来您使用的是旧版本的库(pre GA)。

我建议您下载NuGet中提供的最新Coordinate API - https://www.nuget.org/packages/Google.Apis.Coordinate.v1/
然后,按照get startedOAuth 2.0页面操作。如果缺少某些东西,你应该在那里找到所有正确的文件 - 告诉我们。我们的问题跟踪器Open an issue

答案 1 :(得分:1)

首先需要使用浏览器实例进行身份验证,并且可以重复使用相同的&#34;刷新令牌&#34;对于所有其他请求。我们可以提供一个自定义文件夹位置来存储&#34; auth令牌&#34;并且库将使用该文件夹中存储的令牌。修改后的源代码粘贴在下面:

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