public ActionResult Continue() { var oAuthVerifier = Request.QueryString["oauth_verifier"]; var realmId = Convert.ToInt64(Request.QueryString["realmId"]); var oAuthDataSource = Request.QueryString["dataSource"]; string sAccessToken, sAccessTokenSecret; try { var oAuthSession = getOAuthSession(); IToken accessToken = oAuthSession.ExchangeRequestTokenForAccessToken((IToken)Session["OAuthRequestToken"], oAuthVerifier); sAccessToken = accessToken.Token; sAccessTokenSecret = accessToken.TokenSecret; Session["OAuthRequestToken"] = null; } catch (Intuit.Ipp.Exception.FaultException ex) { throw ex; } catch (Intuit.Ipp.Exception.InvalidTokenException ex) { throw ex; } catch (Intuit.Ipp.Exception.SdkException ex) { throw ex; } if (sAccessToken != null && sAccessTokenSecret != null) { using (var ctx = new QBAppMVC5Entities()) { var newProfile = new OAuthProfile() { AccessToken = Utility.Encrypt(sAccessToken, ConfigurationManager.AppSettings["StorageSecurityKey"]), AccessSecret = Utility.Encrypt(sAccessTokenSecret, ConfigurationManager.AppSettings["StorageSecurityKey"]), Datasource = oAuthDataSource, RealmId = realmId }; ctx.OAuthProfiles.Add(newProfile); var currentUser = ctx.AspNetUsers.SingleOrDefault(u => u.UserName == User.Identity.Name); if (currentUser != null) { currentUser.OAuthProfile = newProfile; } ctx.SaveChanges(); } } // Save... if (Session["OAuthReturnURL"] != null) { var sReturnURL = (string)Session["OAuthReturnURL"]; Session["OAuthReturnURL"] = null; return(Redirect(sReturnURL)); } return(View()); }
// A very basic call (well, 3 calls) to the QB API to find customers with an 'e' somewhere in their name. // Lacks basic robustness (proper exception handling), and obviously the OAuth setup should be abstracted away in (at least) a helper class, // if not a full repository/data layer for API access. // // A non-trivial implementation would also hive the .edmx (data model) away in a separate DAL. public ActionResult CallQuickbooks() { var exampleResult = "Authorisation problem."; Models.ApplicationUser userModel = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>() .FindById(User.Identity.GetUserId()); string accessToken = null, accessSecret = null; long realmID = -1; using (var ctx = new QBAppMVC5Entities()) { var oauthProfile = ctx.OAuthProfiles.SingleOrDefault(p => p.Id == userModel.OAuthProfileId); if (oauthProfile != null) // 'Guaranteed' given that [QuickBooksAuthorize] ensures a non-null userModel.OAuthProfileId. { accessToken = oauthProfile.AccessToken; accessSecret = oauthProfile.AccessSecret; realmID = oauthProfile.RealmId; } } if (accessToken != null) { try { var oAuthRequestValidator = new OAuthRequestValidator( // this is an Intuit object Utility.Decrypt(accessToken, ConfigurationManager.AppSettings["StorageSecurityKey"]), Utility.Decrypt(accessSecret, ConfigurationManager.AppSettings["StorageSecurityKey"]), ConfigurationManager.AppSettings["QBConsumerKey"], ConfigurationManager.AppSettings["QBConsumerSecret"]); var serviceContext = new ServiceContext(realmID.ToString(), IntuitServicesType.QBO, oAuthRequestValidator); QueryService <Customer> queryService = new QueryService <Customer>(serviceContext); var dummyMatch = "e"; var cs = queryService.ExecuteIdsQuery($"select * from customer where displayname like '%{dummyMatch}%'"); var cs2 = queryService.ExecuteIdsQuery($"select * from customer where givenname like '%{dummyMatch}%'"); var cs3 = queryService.ExecuteIdsQuery($"select * from customer where familyname like '%{dummyMatch}%'"); var allCs = cs.Concat(cs2).Concat(cs3).GroupBy(c => c.Id).Select(g => g.First()); var top5cs = allCs.Take(5); exampleResult = "No customers found with 'e' in their names!"; if (top5cs.Count() > 0) { exampleResult = string.Empty; foreach (var c in top5cs) { exampleResult += $"{c.DisplayName} ({c.GivenName} {c.FamilyName});"; } if (exampleResult.Length > 0) { exampleResult = "Max 5 customers found with 'e' in their name: " + exampleResult; } if (exampleResult.EndsWith(";")) { exampleResult = exampleResult.Substring(0, exampleResult.Length - 1); } } } catch (Exception qbEx) { exampleResult = "Exception encountered while calling the Quickbooks API: " + qbEx.ToString(); } } return(View(model: exampleResult)); }