c# - Importing google contacts -


hi want retrieve google contacts in default mvc5 application. login using code below.

 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); 

upon confirmation of authentication save accesstoken , refreshtoken aspnetuserclaims table in callbackmethod in account controller

public async task<actionresult> externalloginconfirmation(externalloginconfirmationviewmodel model, string returnurl)         {             if (user.identity.isauthenticated)             {                 return redirecttoaction("index", "manage");             }              if (modelstate.isvalid)             {                 // information user external login provider                 var info = await authenticationmanager.getexternallogininfoasync();         //one things noted when use googleoptions.scope.add("https://www.google.com/m8/feeds"); commented above info 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);         } 

anyway when loged in using google credentials want retrieve google contacts in homecontroller in contacts method using google client libraries in code below.

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

but exception "execution of request failed: https://www.google.com/m8/feeds/contacts/default/full" remote server returned error: (401) unauthorized. mean accesstoken not valid if not valid why. how can google contacts. please have spent 5 days on not successfull. want retrieve google contacts when logged in using owin middleware component usegoogleauthentication()

first , must sure acess token & refersh token has been saved in cliam table , second, should know access token has date fro 1 hour


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -