Gsuite API面临授权问题

时间:2019-04-19 06:22:20

标签: c# google-oauth gsuite

我想使用我的域的Google API创建用户。我遇到以下代码的授权问题。如果有的话,请向我建议任何代码示例。我在下面提供我的代码,请帮帮我。

            string[] Scopes = { DirectoryService.Scope.AdminDirectoryUser };
            string ApplicationName = "App Name";
            UserCredential credential;

            using (var stream =
               new FileStream(Server.MapPath("..\\") + credentials.json",FileMode.Open, FileAccess.Read))
            {

                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = Server.MapPath("..\\") + "token.json";

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

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


            UsersResource usersResource = new UsersResource(service);


            Google.Apis.Admin.Directory.directory_v1.Data.User user = new Google.Apis.Admin.Directory.directory_v1.Data.User();
            UserName name = new UserName();
            name.FamilyName = register.ClientName;
            name.GivenName = register.ClientName;
            user.Name = name;
            user.Password = Password;
            user.PrimaryEmail = ConfigurationManager.AppSettings["CompanyEmailID"].ToString();

            var r = usersResource.Insert(user);
            r.Execute();

0 个答案:

没有答案