Could someone bring Google OAuth2 for Cloud DNS via Rest to light?

369 views Asked by At

Infinite Loop Error.

I have been spending way too much time going in circles in Googles really terrible API documentation regarding implementation of their cloud services in a desktop application.

First major problem, it seems that I must use Oauth2 somehow in order to obtain additional client credentials (which I must update/refresh using a refresh token as this changes as well) -- since apparently the API credentials just aren't enough to do full on communication.

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

Could someone give an example using C# .NET in a WinForms application that uses rest or the Google .NET API library to establish the required OAuth2 token data/etc which will then later be used to access all of these API's here.

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

And please ---- no gcloud.exe stuff. Not practical to expect clients to download that massive and over complicated installer just to get the tool. :)

1

There are 1 answers

9
konqi On BEST ANSWER

found some time to create a small sample application. You can find the code on github .

You said you don't want to use the client_secrets.json. The only quick solution i found was to provide the necessary stuff via memory stream - there may be better ways.

Most of the authentication and Cloud DNS client is done in these lines

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",
    });

The example lacks any good coding style (I could use a Json lib, and i could wrap the writer in using) but the sample should work. I hope this helps in getting you on the right track.

Edit: I deleted the client id and secret used in the sample, so you need to replace those with your own to make the code work.