Google API-身份验证设置和.NET客户端

时间:2020-10-12 13:54:38

标签: api authentication

我正在尝试使用Google API从C#客户端应用程序中实现两件事。

  • 列出我的google目录的所有用户
  • 获取每个用户的所有电子邮件的元数据

但是,看来我的服务帐户配置给我带来了问题。这就是我所做的。

  1. 在console.cloud.google.com中创建的项目
  2. 通过API和服务
  3. 启用了 Admin SDK Gmail API
  4. 创建了具有域委托的服务帐户,并获得了certificate.json文件。

Service Account setup 4.在admin.google.com上,我转到安全 / API控件并管理域范围的委派。从这里,我从我的服务帐户客户端ID添加了新的API客户端,并分配了以下范围:

https://www.googleapis.com/auth/gmail.settings.basic

https://www.googleapis.com/auth/gmail.settings.sharing

https://www.googleapis.com/auth/admin.directory.user.readonly

enter image description here

最后,当我想尝试检索用户列表时,出现错误:预期的OAuth 2访问令牌,登录cookie或其他有效的身份验证凭据

 static void Main(string[] args)
    {
        
        using (var stream =
            new FileStream("..\\..\\..\\credentials.json", FileMode.Open, FileAccess.Read))
        {
        var credential = GoogleCredential.FromFile("..\\..\\..\\credentials.json");

            var dirservice = new DirectoryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "testbuildingnomiproductname",
            });

            var listReq = dirservice.Users.List();
            
            Users allUsers = listReq.Execute();

            int counter = 0;
            foreach (User myUser in allUsers.UsersValue)
            {
                Console.WriteLine("*" + myUser.PrimaryEmail);
                counter++;
            }

            Console.WriteLine(counter);
            Console.ReadKey();

        }
    }

0 个答案:

没有答案
相关问题