Search code examples
google-apigoogle-oauthgoogle-api-dotnet-clientservice-accountsgoogle-admin-settings-api

Google Admin Setttings API connection for .NET


I've been working with the Google directory API for quite some time now.

However, I need to update SSO settings in the admin settings section of Google. Yes, they say it will be deprecated at some point, but according to a google employee, it's going to be a while before a new API is available and then the old one will be removed.

First, if there is a NUGET package out there, please let me know. I can't seem to find anything that works with the admin settings API: https://developers.google.com/admin-sdk/admin-settings/

My first attempt is getting the SSO settings in Google.

I can use postman to pull this information so I know the API works.

However, I'm running into two issues:

  1. How can I authenticate using the service certificate that I use in the apis.google.directory class?
  2. Anticipating, how do I request access to the admin settings? In directory API, I have the scope enum to select from. If I'm making a manual connection to the API I assume I'll need to call this by hand?

Code

        var certificate = new X509Certificate2(serviceAccountCertPath,
                                               serviceAccountCertPassword,
                                               X509KeyStorageFlags.Exportable);

    // below the scopes are going to get in my way, right?  What is the scope process I need to do for this manually?  
       credential = new ServiceAccountCredential(
                    new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser, 
                                     DirectoryService.Scope.AdminDirectoryGroup,
                                     DirectoryService.Scope.AdminDirectoryOrgunit},
                        User = _szAdminEmail
                    }.FromCertificate(certificate));

            // I'm not seeing anyway to call the above credentials 
            using (HttpClient client = new HttpClient())
            {

//                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
                client.BaseAddress = new Uri(@"https://apps-apis.google.com/a/feeds/domain/2.0/[mydomain]/sso/general");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //client.DefaultRequestHeaders.
                HttpResponseMessage response = client.GetAsync("api/Values").Result;  // Blocking call!    
                var products = response.Content.ReadAsStringAsync().Result;
                return products.ToString();
            }

Solution

  • The admin settings API does not appear to support service account authentication you will need to use Oauth2. Admin Settings Oauth

    Your not going to be able to use it very easily using the Google .net client library as that library was designed for use with the Google discovery apis. I dont think the Admin Settings API is a discovery api. You might be able to use the old gdata library for it I am not sure if one exists I have not been able to find it on nuget. If you do find it the old gdata library doesn't support oauth2 which means that you will need to use the new library for that and plug in the gdata library after.

    I have only done this before using the Google contacts api I have a tutorial here on how i did it it may help you here

    Auth

    string clientId = "xxxxxx.apps.googleusercontent.com";
    string clientSecret = "xxxxx";
    
    
    string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" };     // view your basic profile info.
    try
    {
        // Use the current Google .net client library to get the Oauth2 stuff.
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                     , scopes
                                                                                     , "test"
                                                                                     , CancellationToken.None
                                                                                     , new FileDataStore("test")).Result;
    
        // Translate the Oauth permissions to something the old client libray can read
        OAuth2Parameters parameters = new OAuth2Parameters();
        parameters.AccessToken = credential.Token.AccessToken;
        parameters.RefreshToken = credential.Token.RefreshToken;
        RunContactsSample(parameters);
    

    If you cant find the gdata library for it you may have better luck just using the library for authencation and then code the rest of the calls yourself. It returns xml not json.