有人可以通过Rest将Google OAuth2 for Cloud DNS带到曝光

本文关键字:DNS Cloud 曝光 for OAuth2 可以通过 Rest Google | 更新日期: 2023-09-27 17:55:07

无限循环错误。

我一直在谷歌关于在桌面应用程序中实现其云服务的非常糟糕的 API 文档中兜圈子。

第一个主要问题,似乎我必须以某种方式使用 Oauth2 才能获得额外的客户端凭据(我必须使用刷新令牌进行更新/刷新,因为这也会发生变化)——因为显然 API 凭据不足以完成完整的通信。

https://cloud.google.com/dns/api/authorization

有人可以举一个在WinForms应用程序中使用C# .NET的示例,该应用程序使用rest或Google .NET API库来建立所需的OAuth2令牌数据/等,然后稍后将用于访问所有这些API。

https://cloud.google.com/dns/api/v1/managedZones

请不要----gcloud.exe的东西。期望客户下载庞大且过于复杂的安装程序只是为了获得该工具是不切实际的。 :)

有人可以通过Rest将Google OAuth2 for Cloud DNS带到曝光

花了一些时间创建了一个小型示例应用程序。你可以在 github 上找到代码。

说你不想使用 client_secrets.json。我发现的唯一快速解决方案是通过内存流提供必要的东西 - 可能有更好的方法。

大多数身份验证和云 DNS 客户端都是在这些行中完成的

using (var memstream = new MemoryStream())
{
    // Ugly way of providing clientId & Secret inmemory
    var writer = new StreamWriter(memstream);
    writer.Write(@"{""installed"":{""auth_uri"":""https://accounts.google.com/o/oauth2/auth"",""client_secret"":""");
    writer.Write(clientSecret);
    writer.Write(@""",""token_uri"":""https://accounts.google.com/o/oauth2/token"",""client_email"":"""",""redirect_uris"":[""urn:ietf:wg:oauth:2.0:oob"",""oob""],""client_x509_cert_url"":"""",""client_id"":""");
    writer.Write(clientId);
    writer.Write(@""",""auth_provider_x509_cert_url"":""https://www.googleapis.com/oauth2/v1/certs""}}");
    writer.Flush();
    memstream.Position = 0;
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(memstream).Secrets,
        new[] { DnsService.Scope.NdevClouddnsReadwrite},
        "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
}
// Create the service.
var service = new DnsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "Cloud DNS API Sample",
    });

该示例缺乏任何良好的编码风格(我可以使用 Json 库,并且可以将编写器包装为使用),但示例应该可以工作。我希望这有助于您走上正轨。

编辑:我删除了示例中使用的客户端 ID 和机密,因此您需要将它们替换为自己的 ID 和机密以使代码正常工作。