/// <summary> /// Redirects an administrator from the admin site to Vismas authentication server to get an access token. /// </summary> /// <returns> /// <c>True</c> if it is successful. /// </returns> /// <param name="customer">A user gotten from the Visma eAccounting API.</param> public bool RequestToken() { try { // Creates a list of scopes needed to access the right token var scopes = new List <string> { "offline_access", "ea:api", "ea:sales" }; // Gets the redirect URI in this application from the web.config file var redirectUri = new Uri(WebConfigurationManager.AppSettings["RedirectUri"]); // Creates a new WebServerClient var client = VismaAuthProvider.CreateClient(); // Requests a brand new access token from Visma client.RequestUserAuthorization(scopes, redirectUri); return(true); } catch (Exception e) { // Creates a new error filer object var errorLog = new ErrorFiler(); // Logs the error to the error log, with the name of the exception and where it occured errorLog.WriteError(e.GetType().FullName, "NasBLL, bool AskForToken()"); return(false); } }
/// <summary> /// This is the endpoint of the redirect URI given to Visma, which receives the code to be used for requesting an access token from the authorization server. /// </summary> /// <returns> /// On successful saving of the access token the user is redirected home, or to an error site if something went wrong. /// </returns> /// <remarks> /// <para>The method asks for an authorization token from the server and stores it in the database.</para> /// </remarks> public ActionResult Index() { var client = VismaAuthProvider.CreateClient(); var state = VismaAuthProvider.RequestAuthorization(client); var successUpdateVismaToken = _db.UpdateVismaToken(state); if (successUpdateVismaToken) { return(RedirectToAction("Index", "Home")); } return(RedirectToAction("Error", "Home")); }
/// <summary> /// Requests a list of customers from Visma, using a valid token. /// </summary> /// <returns> /// List of invoices /// </returns> public CustomerResponse GetCustomerListFromVisma() { // Creates a new WebServerClient var client = VismaAuthProvider.CreateClient(); // Gets the access token stored in the database var dbToken = _db.GetToken(); // Sends a request for a list of customers to the Visma eAccounting web api and stores it in the customerList variable var customerList = VismaAuthProvider.GetProtectedResource <CustomerResponse>(client, dbToken, "/v2/customers"); // Requests a refreshed token from Vismas servers using the refreshtoken from the last token var refreshedToken = VismaAuthProvider.RequestAuthorizationRefresh(dbToken); // Updates the token in the database _db.UpdateVismaToken(refreshedToken); return(customerList); }