【发布时间】:2014-10-08 10:45:55
【问题描述】:
我希望有人可以帮助我。
请看下面的源代码。当 Execute 最后被击中时。我会收到以下错误:
Google.Apis.Auth.OAuth2.Responses.TokenResponseException 未处理 _HResult=-2146233088 _message=Error:"access_denied", Description:"请求的客户端未授权。", Uri:""
我已经尝试了很多东西。创建用户、列表组等(使用正确的范围),但每次我都遇到同样的错误。
我创建了一个服务帐户,并且 Google Developers Console 中的所有 API 都设置为“开启” 我用于模拟的用户是完全管理员。
我已经尝试了几件事,但我必须做一些愚蠢的事情。 该代码看起来很像 Stack Overflow 上的另一个问题:Google Admin SDK Unable to Create User - Exception 403 Forbidden 但我的行不通。谁能告诉我我忘记了什么?
private static readonly string[] Scopes = new string[] { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryGroup, DirectoryService.Scope.AdminDirectoryGroupMember };
static void Main(string[] args)
{
String serviceAccountEmail = "***@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"file.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = Scopes,
User = "admin email address",
}.FromCertificate(certificate));
var service = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "User Provisioning",
});
User newuserbody = new User();
UserName newusername = new UserName();
newuserbody.PrimaryEmail = "bbacon@correct.domain";
newusername.GivenName = "Bob";
newusername.FamilyName = "Bacon";
newuserbody.Name = newusername;
newuserbody.Password = "iambacon";
Google.Apis.Admin.Directory.directory_v1.Data.User results = service.Users.Insert(newuserbody).Execute();
Console.WriteLine("Press any key to continue!");
Console.ReadKey();
}
【问题讨论】:
标签: google-admin-sdk google-api-dotnet-client