我正试图使用谷歌日历API V3来获取我的日历,使用c#与服务帐户其给出错误
发送直接消息或获取响应时发生错误。
下面是我的代码。
private const string SERVICE_ACCOUNT_EMAIL = "XXX@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"D:privatekey.p12";
static void Main(string[] args)
{
Test();
}
static void Test()
{
X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
AssertionFlowClient provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
Scope = "Calendar",
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
CalendarService c1 =new CalendarService(
new BaseClientService.Initializer()
{
Authenticator = auth
});
c1.Calendars.Get("MyCalenderID").Fetch();
}
你的作用域应该是:
"https://www.googleapis.com/auth/" +
Google.Apis.Calendar.v3.CalendarService.Scopes.CalendarReadonly.ToString().ToLower()