您好我想在我的默认MVC5应用程序中检索谷歌联系人。 我使用下面的代码成功登录。
GoogleOAuth2AuthenticationOptions googleOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = "<ClientId>",
ClientSecret = "<ClientSecret>",
AccessType = "offline",
Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new Claim("accessToken", context.AccessToken))
if (context.RefreshToken != null)
{
context.Identity.AddClaim(new Claim("refreshToken", context.RefreshToken));
}
context.Identity.AddClaim(new Claim("EmailAddressFieldName", context.Email));
context.Identity.AddClaim(new Claim("NameFieldName", context.Name));
context.Identity.AddClaim(new Claim("TokenIssuedFieldName", DateTime.Now.ToString()));
context.Identity.AddClaim(new Claim("TokenExpiresInFieldName",
((long)context.ExpiresIn.Value.TotalSeconds).ToString()));
return Task.FromResult(0);
}
}
};
//googleOptions.Scope.Add("https://www.google.com/m8/feeds");
app.UseGoogleAuthentication(googleOptions);
确认身份验证后,我将AccessToken和RefreshToken保存到帐户控制器的callback方法中的ASPNetUserClaims表中
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Manage");
}
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
//one things to be noted is when i use googleOptions.Scope.Add("https://www.google.com/m8/feeds"); as commented above this info is null
//not sure why
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
var accessToken = info.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == "accessToken");
var refreshToken = info.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == "refreshToken");
await UserManager.AddClaimAsync(user.Id, accessToken);
await UserManager.AddClaimAsync(user.Id, refreshToken);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
无论如何,当我现在使用谷歌凭证时,我想在联系方式中使用谷歌客户端库检索HomeController中的谷歌联系人,如下面的代码所示。
public ActionResult Contact()
{
var user= UserManager.FindByEmailAsync(User.Identity.Name);
var claim = ClaimsPrincipal.Current.Claims;
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.AccessToken = claim.Where(i => i.Type == "accessToken").Select(i => i.Value).SingleOrDefault();
parameters.RefreshToken = claim.Where(i => i.Type == "refreshToken").Select(i => i.Value).SingleOrDefault();
//parameters.Scope = "https://www.google.com/m8/feeds";
GetGoogleContacts(parameters);
return View();
}
private static void GetGoogleContacts(OAuth2Parameters parameters)
{
try {
RequestSettings settings = new RequestSettings("MvcAuth",parameters);
ContactsRequest cr = new ContactsRequest(settings);
Feed<Contact> f = cr.GetContacts();
foreach (Contact c in f.Entries)
{
Console.WriteLine(c.Name.FullName);
}
}
catch (Exception a)
{
Console.WriteLine("A Google Apps error occurred.");
Console.WriteLine();
}
}
但我得到了例外 “执行请求失败:https://www.google.com/m8/feeds/contacts/default/full” 远程服务器返回错误:(401)未经授权。 这意味着我的accesstoken无效,如果它不是为什么有效。我该如何获得谷歌联系人。请帮助我花了将近5天的时间 但没有成功。我只是想在使用Owin中间件组件登录时检索谷歌联系人UseGoogleAuthentication()
答案 0 :(得分:0)
首先,你必须确保获取令牌和Refersh Token已保存在cliam Table中, 其次,您应该知道访问令牌的日期为1小时