gpt4 book ai didi

c# - 在 C# 中与 Google Admin SDK 集成

转载 作者:太空狗 更新时间:2023-10-29 20:15:38 25 4
gpt4 key购买 nike

我目前正在尝试通过 C# 与 Google Admin SDK 集成,以便我们可以通过我们自己的系统管理用户。但是,在运行项目时出现错误:未经授权的客户端。

我已经通过 super 管理员帐户完成的事情:

这是我正在使用的代码。

ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(_googleServiceSettings.Client_Email)
{
ProjectId = _googleServiceSettings.Project_Id,
User = "superadmin@google.com",
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser }
}.FromPrivateKey(_googleServiceSettings.Private_Key));

var service = new DirectoryService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Test API"
});

var request = service.Users.Get("user@google.com");
var result = await request.ExecuteAsync();

我得到的完整错误是

An unhandled exception has occurred while executing the request. Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.", Uri:""

最佳答案

将打印有关用户的一些信息的示例代码。

重要的项目是类 Google.Apis.Admin.Directory.directory_v1.Data.User

文档 link .

您的错误是由于没有正确创建凭据造成的。通常,创建凭据时范围存在问题。我假设您已为服务帐户正确设置全域委派。

我还假设您模拟的用户是 G Suite super 管理员。否则,您将看到 service.Users.Get() 的 403 错误。

service_account.json 文件是您从 Google 控制台下载(或使用 gcloud 创建)的普通 JSON 文件。

用户 user1@example.com 是将显示其信息的 G Suite 用户的电子邮件地址。

用户 admin@example.com 是 G Suite super 管理员。

using Google.Apis.Auth.OAuth2;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Services;
using System;
using System.IO;

// dotnet add package Google.Apis.Admin.Directory.directory_v1
// Tested with version 1.39.0.1505

// Google.Apis.Admin.Directory.directory_v1.Data.User
// https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/csharp/latest/classGoogle_1_1Apis_1_1Admin_1_1Directory_1_1directory__v1_1_1Data_1_1User.html

namespace Example
{
class Program
{
static void Main(string[] args)
{
// Service Account with Domain-Wide delegation
var sa_file = "service_account.json";

// G Suite User to impersonate
var user_email = "admin@example.com";

// G Suite User to get information about
var gs_email = "user1@example.com";

// Scopes
var scopes = "https://www.googleapis.com/auth/admin.directory.user";

var credential = GoogleCredential.FromFile(sa_file)
.CreateScoped(scopes)
.CreateWithUser(user_email);

// Create Directory API service.
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});

try {
var request = service.Users.Get(gs_email);

var result = request.Execute();

Console.WriteLine("Full Name: {0}", result.Name.FullName);
Console.WriteLine("Email: {0}", result.PrimaryEmail);
Console.WriteLine("ID: {0}", result.Id);
Console.WriteLine("Is Admin: {0}", result.IsAdmin);
} catch {
Console.WriteLine("User not found.");
}
}
}
}

关于c# - 在 C# 中与 Google Admin SDK 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com