适用于.NET的Google Admin Setttings API连接

问题描述 投票:1回答:1

我已经使用Google目录API一段时间了。

但是,我需要在Google的管理设置部分更新SSO设置。是的,他们说它会在某个时候被弃用,但根据谷歌员工的说法,在新的API可用之前会有一段时间,然后旧的API将被删除。

首先,如果那里有NUGET包,请告诉我。我似乎无法找到任何适用于管理员设置API的任何内容:https://developers.google.com/admin-sdk/admin-settings/

我的第一次尝试是在Google中获取SSO设置。

我可以使用postman来提取这些信息,因此我知道API有效。

但是,我遇到了两个问题:

  1. 如何使用我在apis.google.directory类中使用的服务证书进行身份验证?
  2. 预计,如何请求访问管理员设置?在目录API中,我有范围枚举可供选择。如果我正在手动连接API,我假设我需要手动调用它?

        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();
            }
google-api google-oauth google-api-dotnet-client service-accounts google-admin-settings-api
1个回答
1
投票

管理员设置API似乎不支持您需要使用Oauth2的服务帐户身份验证。 Admin Settings Oauth

您无法使用Google .net客户端库轻松使用它,因为该库旨在与Google发现API一起使用。我不认为Admin Settings API是一个发现API。您可能能够使用旧的gdata库我不确定是否存在我无法在nuget上找到它。如果你确实找到它,旧的gdata库不支持oauth2,这意味着你需要使用新的库并插入gdata库。

我在使用谷歌联系人api之前只做了这个我在这里有一个关于我如何做它的教程它可以帮助你here

验证

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);

如果您无法找到gdata库,那么您可以更好地使用该库进行身份验证,然后自己编写其余的调用代码。它返回xml而不是json。

© www.soinside.com 2019 - 2024. All rights reserved.