【问题标题】:Google Coordinate API authentication offline离线 Google Coordinate API 身份验证
【发布时间】:2014-12-01 21:30:33
【问题描述】:

尝试对 Google 坐标 api 进行身份验证。尝试使用服务帐户进行服务帐户身份验证,并使用this 问题发布堆栈流。找到this anwer 并很好地描述了我的问题。

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

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

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

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

我可以使用下面的代码 sn-p 来读取 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();

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

【问题讨论】:

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


    【解决方案1】:

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

    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

    【讨论】:

      【解决方案2】:

      看起来您使用的是非常旧的库版本(GA 之前)。

      我建议您下载最新的 Coordinate API,它在 NuGet 中可用 - https://www.nuget.org/packages/Google.Apis.Coordinate.v1/
      然后,关注get startedOAuth 2.0 页面。如果缺少某些内容,您应该在那里找到所有正确的文档 - 让我们知道。 Open an issue 在我们的问题跟踪器中。

      【讨论】:

      • 我已经用最新的代码更新了我的问题。请看看
      • 我没有看到你在 service.Location.List(...) 对象上调用了 Execute 方法,这是为什么呢?第二件事 - 看起来你需要一个服务帐户解决方案 - 看看这个文档 - developers.google.com/api-client-library/dotnet/guide/…
      • 服务帐户不能与 Coordinate API 一起使用。 [这是因为 Coordinate API 要求经过身份验证的 API 用户拥有 Coordinate 许可,但无法将 Coordinate 许可附加到服务帐户]
      • 我需要从新版本的 GA 中获取代码示例,以运行此处所说的解决方案 stackoverflow.com/questions/17177262/…
      • OK.. 抱歉 :( 所以您必须对用户进行一次身份验证,然后重用刷新令牌以保持您的访问令牌保持刷新和有效(库会为您处理)跨度>
      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      相关资源
      最近更新 更多