我正在尝试使用Google Calendar API V3来获取我的日历,使用C#with Service帐户会出错
发送直接消息或收到回复时出错。
以下是我的代码。
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();
}
答案 0 :(得分:-1)
你的范围应该是:
"https://www.googleapis.com/auth/" +
Google.Apis.Calendar.v3.CalendarService.Scopes.CalendarReadonly.ToString().ToLower()