1

我使用 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();
            }
4

1 回答 1

1

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

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

我只是在使用 Google 联系人 api 之前完成了这项 工作

认证

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。

于 2019-02-21T10:27:27.877 回答