适用于MVC的Google Calendar API身份验证

时间:2015-04-30 17:59:57

标签: 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 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
        };
    }

0 个答案:

没有答案