有人可以通过 Rest 为 Cloud DNS 带来 Google OAuth2 吗?
Could someone bring Google OAuth2 for Cloud DNS via Rest to light?
无限循环错误。
我花了太多时间在 Google 关于在桌面应用程序中实施云服务的非常糟糕的 API 文档中兜圈子。
第一个主要问题,似乎我必须以某种方式使用 Oauth2 以获得额外的客户端凭据(我必须 update/refresh 使用刷新令牌,因为这也发生了变化)——因为显然 API 凭据不足以进行全面沟通。
https://cloud.google.com/dns/api/authorization
有人可以举例说明在使用 rest 或 Google .NET API 库的 WinForms 应用程序中使用 C# .NET 来建立所需的 OAuth2 令牌 data/etc 然后稍后用于在此处访问所有这些 API。
https://cloud.google.com/dns/api/v1/managedZones
请 ---- 没有 gcloud.exe 东西。期望客户为了获得该工具而下载庞大且过于复杂的安装程序是不切实际的。 :)
抽出一些时间来创建一个小的示例应用程序。您可以在 github 上找到代码。
你说你不想使用 client_secrets.json。我找到的唯一快速解决方案是通过内存流提供必要的东西——可能有更好的方法。
大部分身份验证和 Cloud 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 和密码,因此您需要将它们替换为您自己的才能使代码正常工作。
无限循环错误。
我花了太多时间在 Google 关于在桌面应用程序中实施云服务的非常糟糕的 API 文档中兜圈子。
第一个主要问题,似乎我必须以某种方式使用 Oauth2 以获得额外的客户端凭据(我必须 update/refresh 使用刷新令牌,因为这也发生了变化)——因为显然 API 凭据不足以进行全面沟通。
https://cloud.google.com/dns/api/authorization
有人可以举例说明在使用 rest 或 Google .NET API 库的 WinForms 应用程序中使用 C# .NET 来建立所需的 OAuth2 令牌 data/etc 然后稍后用于在此处访问所有这些 API。
https://cloud.google.com/dns/api/v1/managedZones
请 ---- 没有 gcloud.exe 东西。期望客户为了获得该工具而下载庞大且过于复杂的安装程序是不切实际的。 :)
抽出一些时间来创建一个小的示例应用程序。您可以在 github 上找到代码。
你说你不想使用 client_secrets.json。我找到的唯一快速解决方案是通过内存流提供必要的东西——可能有更好的方法。
大部分身份验证和 Cloud 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 和密码,因此您需要将它们替换为您自己的才能使代码正常工作。