【发布时间】:2021-08-10 19:04:27
【问题描述】:
尝试使用 NuGet 包 Google.Apis.Gmail.v1 访问我的 Gmail 收件箱。我不想使用 OAuth。我确实想使用 API 密钥。
于是我去获取了一个 API 密钥:https://developers.google.com/api-client-library/dotnet/guide/aaa_apikeys
然后我去这里了解如何使用 API 密钥进行授权:https://developers.google.com/api-client-library/dotnet/get_started
然后我得到了这个:
var service = new GmailService(new BaseClientService.Initializer
{
ApplicationName = "NAME OF MY APP HERE",
ApiKey = "MY API KEY HERE"
});
var inboxlistRequest = service.Users.Messages.List("mygmail@gmail.com");
inboxlistRequest.LabelIds = "INBOX";
inboxlistRequest.IncludeSpamTrash = true;
var emailListResponse = inboxlistRequest.Execute();
当最后一行执行时,我得到了这个:
Google.GoogleApiException:Google.Apis.Requests.RequestError 请求缺少所需的身份验证凭据。预期的 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。见https://developers.google.com/identity/sign-in/web/devconsole-project.
at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
...
为什么它不起作用?对哪些 API 密钥可以访问有一些限制吗?哪里写的?
【问题讨论】:
-
除了您的应用程序之外,没有与您的 API 密钥相关联的身份。但是您的应用程序不是 mygmail@gmail.com,因此无法访问邮箱。你必须使用 OAuth 来做你想做的事。
-
2022 年:此 API 不支持 API 密钥。预期的 OAuth2 访问令牌或其他声明主体的身份验证凭据。