我如何使用一个已经验证的会话谷歌日历



我已经在我的MVC5应用程序上实现了简单的Google+身份验证,我想访问他们的Google日历。我如何使用MVC标识系统和我已经认证的用户做到这一点?

Dim authGOps = New GooglePlusAuthenticationOptions() With {
        .Caption = "Google+",
        .ClientId = "MYCLIENTRID",
        .ClientSecret = "MYCLIENTSECRET",
        .Provider = New GooglePlusAuthenticationProvider() With {
            .OnAuthenticated = Async Function(context)
                                   context.Identity.AddClaim(New Claim(GooglePlusAccessTokenClaimType, context.AccessToken))
                               End Function
        }
    }

    authGOps.Scope.Add("https://www.googleapis.com/auth/calendar")
    app.UseGooglePlusAuthentication(authGOps)

获取日历服务:

Dim calendarService = New CalendarService(New Google.Apis.Services.BaseClientService.Initializer() With {
WHAT GOES HERE TO AUTHENTICATE USING MY OLD AUTH CEDENTIALS?
} 

所以我也很喜欢使用服务,因为它几乎到处都有记录,但我发现了一个解决方案,至少可以获得数据,而不必再次登录。

确保Nuget Json。Net来反序列化和强类型。否则你将得到一个Json字符串来管理。

是用c#写的,但我相信翻译起来不会太难。希望能有所帮助!

var claimsIdentity = User.Identity as ClaimsIdentity;
var claims = claimsIdentity.Claims;
var accessTokenClaim = claims.FirstOrDefault(x => x.Type == GooglePlusAccessTokenClaimType);
if (accessTokenClaim != null)
{
    string calendarUrl = "https://www.googleapis.com/calendar/v3/users/me/calendarList?access_token=" + Uri.EscapeDataString(accessTokenClaim.Value);
    using(var client = new HttpClient())
    {
        var response = await client.GetAsync(calendarUrl);
        var calendarList = JsonConvert.DeserializeObject<CalendarList>(await response.Content.ReadAsStringAsync());
    }
}

相关内容

  • 没有找到相关文章

最新更新