0
您好我想在我的默認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到ASPNetUserClaims表我callbackmethod在賬戶控制器
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);
}
反正當我在使用Google憑據現在我想要檢索的谷歌聯繫人正在loged聯繫方式中的HomeController使用下面代碼中的google客戶端庫。
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()
什麼是ContactsRequest您如何應用訪問令牌? – DaImTo
我正在使用.Net客戶端庫進行ContactsRequest請參閱下面的鏈接https://developers.google.com/google-apps/contacts/v3/,我將accessToken作爲OAuth2Parameters傳遞給 –
實際上,我正在遵循本教程http:// www.daimto.com/google-contacts-with-c/但是我從Aspnet獲取accessToken聲明,當使用owin中間件組件成功進行身份驗證時,我存儲在外部回調函數中usegoogleauthentication() –