我如何使用已经过身份验证的谷歌日历会话?

时间:2014-06-11 18:04:18

标签: asp.net vb.net asp.net-mvc-5 asp.net-identity

我已经在我的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?
} 

1 个答案:

答案 0 :(得分:0)

所以我也很乐意使用该服务,因为它几乎随处可见,但我找到了一种解决方法,至少可以获取数据,而无需再次登录。

确保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());
    }
}