【问题标题】:AdminSettings API using service account in a C# Console application在 C# 控制台应用程序中使用服务帐户的 AdminSettings API
【发布时间】:2016-06-21 06:56:42
【问题描述】:

我正在尝试将 Google Admin Settings API 与服务帐户一起使用,但在 C# 控制台应用程序中没有成功。

据我了解,我首先必须获得一个 OAuth 令牌。为此,我成功地尝试了 2 种方法:使用 Google.Apis.Auth.OAuth2.ServiceAccountCredentials 或手动创建 JWT 断言。

但是当我使用 OAuth 令牌(例如 maximumNumberOfUsers)调用管理设置 API 时,我总是收到 403 错误,并显示“您无权对域 xxx 执行操作”消息。

我下载了GAM,因为作者也调用了这个API,这样我就可以编写相同的HTTP请求。就像 GAM wiki 中解释的那样,我按照所有步骤创建了一个新的服务帐户和一个新的 OAuth 客户端 ID,这样我就可以确定这不是范围问题。我还激活了 Jay Lee in this thread 提出的调试模式。就像线程 cmets 中解释的那样,它仍然无法使用我的 OAuth 令牌,但使用 GAM OAuth 令牌调用 API 成功。

所以它似乎与 OAuth 令牌本身有关。创建 OAuth 令牌时遇到的一个问题是我无法指定“子”属性(或 ServiceAccountCredentials 的用户)。如果我添加它,我会收到一个 403 Forbidden 响应,其中包含“请求的客户端未授权”。在生成令牌时作为 error_description,即在调用 API 之前。所以也许这是问题所在,但我不知道如何解决它,因为我使用的是管理员电子邮件。

另一种可能性是此 API 需要 OAuth 客户端凭据,因为 GAM 需要 2 种不同类型的凭据,服务帐户和 OAuth 客户端。由于我只能在我的项目中使用服务帐户凭据,如果是这种情况,我恐怕会被卡住......

我没有看到其他选项,而且我都被这两个选项卡住了,所以任何帮助表示赞赏。谢谢!

我的代码:

public static string GetEnterpriseUsersCount()
{
    string domain = MYDOMAIN;

    string certPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
    certPath = certPath.Substring(0, certPath.LastIndexOf("\\") + 1) + "GAMCreds.p12";

    var certData = File.ReadAllBytes(certPath);
    X509Certificate2 privateCertificate = new X509Certificate2(certData, "notasecret", X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
        {
            Scopes = new[] { "https://apps-apis.google.com/a/feeds/domain/" },
            User = ADMIN_EMAIL
        }.FromCertificate(privateCertificate));

    Task<bool> oAuthRequest = credential.RequestAccessTokenAsync(new CancellationToken());
    oAuthRequest.Wait();


    string uri = string.Format("https://apps-apis.google.com/a/feeds/domain/2.0/{0}/general/maximumNumberOfUsers", domain);

    HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
    if (request != null)
    {
        request.Method = "GET";
        request.Headers.Add("Authorization", string.Format("Bearer {0}", credential.Token.AccessToken));

        // Return the response
        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }
    }

    return null;
}

编辑:我专注于下面 Jay Lee 建议的范围,似乎缺少的范围是“https://www.googleapis.com/auth/admin.directory.domain”。但是,这在 Admin Settings API 文档页面中没有任何内容。至少,我没有找到。 'https://apps-apis.google.com/a/feeds/domain/' 也是必要的,但我已经将它添加到允许范围列表中。谢谢杰!

编辑 2:我还更新了源代码,以便将来提供帮助。

【问题讨论】:

    标签: c# oauth google-admin-sdk google-admin-settings-api


    【解决方案1】:

    您需要授予服务帐号的客户端 ID 访问管理员设置 API 范围的权限。在正确的正确范围内遵循Drive domain wide delegation instructions 除外 sub。然后你可以设置 sub= 而不会出错。

    【讨论】:

      猜你喜欢
      • 2021-08-25
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多