C# 用于MVC的Google日历API身份验证
尝试向当前登录的用户插入新日历时,我遇到以下错误。用户可以登录和注册,但尝试添加新日历时会出现此错误C# 用于MVC的Google日历API身份验证,c#,asp.net-mvc,authentication,google-api,google-calendar-api,C#,Asp.net Mvc,Authentication,Google Api,Google Calendar Api,尝试向当前登录的用户插入新日历时,我遇到以下错误。用户可以登录和注册,但尝试添加新日历时会出现此错误 Google.Apis.Requests.RequestError Insufficient Permission [403] Errors [ Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global] ] 执行以下代码时出错: CalendarService go
Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
执行以下代码时出错:
CalendarService googlecalender = new CalendarService(await (new GoogleAuthentication()).GetInitializer());
Calendar calendar = new Calendar();
calendar.Summary = "sampleCalendar";
calendar.Id = "_Sample";
calendar.Kind = "calendar#calendar";
var calendarRequest = googlecalender.Calendars.Insert(calendar);
var result = calendarRequest.Execute();
我已将google身份验证修改为以下内容:
var authenInfo = new GoogleOAuth2AuthenticationOptions();
authenInfo.Scope.Add("openid");
authenInfo.Scope.Add("profile");
authenInfo.Scope.Add("email");
authenInfo.Scope.Add("https://www.googleapis.com/auth/plus.login");
authenInfo.Scope.Add("https://www.googleapis.com/auth/plus.me");
authenInfo.Scope.Add(YouTubeService.Scope.Youtube);
authenInfo.Scope.Add(CalendarService.Scope.Calendar);
authenInfo.Scope.Add(CalendarService.Scope.CalendarReadonly);
authenInfo.ClientId = "CLIENTID";
authenInfo.ClientSecret = "CLIENTSECRET";
authenInfo.Provider = new GoogleOAuth2AuthenticationProvider();
app.UseGoogleAuthentication(authenInfo);
GoogleAuthentication类是:
public GoogleAuthentication()
{
ClientSecret = new ClientSecrets();
ClientSecret.ClientSecret = "CLIENTSECRET";
ClientSecret.ClientId = "CLIENTID";
Scope = new[] {
"https://www.googleapis.com/auth/plus.login",
YouTubeService.Scope.Youtube,
CalendarService.Scope.Calendar
};
}
private async void Authorize()
{
Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
ClientSecret,
Scope,
"user",
CancellationToken.None
);
}
public async Task<BaseClientService.Initializer> GetInitializer()
{
if (Credential == null)
Authorize();
else if (Credential.Token.IsExpired(SystemClock.Default))
{
Boolean tokenRefreshed = await Credential.RefreshTokenAsync(CancellationToken.None);
}
return new BaseClientService.Initializer()
{
HttpClientInitializer = Credential,
ApplicationName = Properties.Settings.Default.ApplicationName
};
}
publicGoogleAuthentication()
{
ClientSecret=新的ClientSecrets();
ClientSecret.ClientSecret=“ClientSecret”;
ClientSecret.ClientId=“ClientId”;
范围=新[]{
"https://www.googleapis.com/auth/plus.login",
YouTubeService.Scope.Youtube,
CalendarService.Scope.Calendar
};
}
私有异步void Authorize()
{
凭证=等待GoogleWebAuthorizationBroker.AuthorizationAsync(
客户机密,
范围
“用户”,
取消令牌。无
);
}
公共异步任务GetInitializer()
{
如果(凭证==null)
授权();
else if(Credential.Token.IsExpired(SystemClock.Default))
{
Boolean TokenRefresh=await Credential.RefreshTokenAsync(CancellationToken.None);
}
返回新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=Properties.Settings.Default.ApplicationName
};
}
您是否为您的项目启用了对该特定API的访问?请参阅:https://console.developers.google.com/project/[您的项目名称]/APUI/apiview/calendar/overview
403是身份验证失败。oauth2涉及人工操作授权(通常通过web浏览器)和存储授权令牌的程序(通常是文件,以及id和机密)。我不是一个很会说话的人,所以我不知道你是否已经完成了整个循环,尤其是令牌。