Проблема авторизации с Gsuite API - PullRequest
1 голос
/ 19 апреля 2019

Я хочу создать пользователя с помощью 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();
...