【发布时间】:2014-12-12 02:15:22
【问题描述】:
也许我根本没有得到“它”,“它”是完成这项工作所需的整体设置。
我有一个网站可以抓取其他网站的体育赛事。我想根据结果自动创建 Google 日历事件,因此我想在我的 GMail 帐户中的日历上授予我的 Web 应用程序读/写访问权限。
一个星期以来,我一直在努力解决这个问题,但我无法让它发挥作用,这让我作为开发人员的自尊心受到打击。
我“理解”的方式是我需要一个 Google API v3 服务帐户,因为我不需要特定用户的 API 密钥。还是我需要一个简单的 API 密钥(而不是 oAuth)?
无论如何,我使用的是服务帐户。
在我的 HomeController 中,我正在尝试获取日历,所以我知道这一切正常。
public void Calendar()
{
string serviceAccountEmail = "...@developer.gserviceaccount.com";
var certificate = new X509Certificate2(
Server.MapPath("~") + @"\App_Data\key.p12",
"notasecret",
X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential =
new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[]
{
CalendarService.Scope.Calendar
},
User = "MY-GMAIL-EMAIL" // Is this correct?
}
.FromCertificate(certificate));
BaseClientService.Initializer initializer = new BaseClientService.Initializer();
initializer.HttpClientInitializer = credential;
initializer.ApplicationName = "CALENDAR NAME"; // Correct?
var service = new CalendarService(initializer);
var list = service.CalendarList.List().Execute().Items; // Exception :-(
}
我得到的错误:
An exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll but was not handled in user code
Additional information: Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""
所以我在 Google 日历中尝试了很多东西,比如将其公开,将服务帐户电子邮件添加为 READ/WRITE 用户。
我需要做什么来授权我的 Web 应用程序以便它可以代表我创建事件?
【问题讨论】:
-
这个错误很可能是由于访问了日历API。将服务帐户电子邮件地址添加到您的日历中,这可能会使其访问您的日历。检查此链接developers.google.com/admin-sdk/directory/v1/guides/…。此外,如果您不想访问用户日历而不是服务帐户,则可以使用 Oauth
标签: c# asp.net google-api google-calendar-api