【问题标题】:Google Admin Setttings API connection for .NET.NET 的 Google 管理员设置 API 连接
【发布时间】:2019-02-19 20:52:48
【问题描述】:

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

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

首先,如果有 NUGET 包,请告诉我。我似乎找不到任何适用于管理设置 API 的东西:https://developers.google.com/admin-sdk/admin-settings/

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

我可以使用邮递员提取此信息,以便我知道 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】:

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

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

    我只是在使用 Google 联系人 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。

    【讨论】:

    • 我在想可能是这样...谢谢!
    • oauth2 页面底部 apps-apis.google.com/a/feeds/domain 记住这是旧的,它可能是 oauth2 之前的 抱歉,我无法提供更多帮助,我无法访问管理员帐户,因此无法帮助您解决问题。
    • 我在邮递员中使用 oAuth2 来手动运行它,所以我觉得我很好。再次感谢您的 sn-p。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多