允许通过Google Gmail API阅读电子邮件

时间:2015-11-29 12:19:47

标签: c# asp.net-mvc gmail google-apps google-authentication

您好我想阅读所有电子邮件,我创建了一个谷歌应用程序,如下所示 enter image description here

现在我通过以下代码进行测试。

public async Task ListLabels()
        {
            UserCredential credential = null; ;
            using (var stream = new FileStream("client_secret_899062187155-oog0nddbvujerg5pdklp24clt25rg9ut.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                    new[] { GmailService.Scope.GmailReadonly },
                    "user", CancellationToken.None);
            }

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

            try
            {
                ListLabelsResponse response = service.Users.Labels.List("me").Execute();
                foreach (Label label in response.Labels.OrderBy(p => p.Name))
                {
                    Console.WriteLine(label.Id + " - " + label.Name);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
            }
        }

现在,我登录后出现错误

enter image description here

我无法确定需要设置的重定向网址。

1 个答案:

答案 0 :(得分:-1)

创建项目选择应用程序类型为Other时,然后创建您的凭据将起作用:

see image