Esempio n. 1
0
        /// <summary>
        /// Requests a protecte reource, based on the path provided, from the Visma eAccounting API.
        /// </summary>
        /// <returns>
        /// A protected resource from Visma eAccounting. In this app that means a list of customers or invoices.
        /// </returns>
        /// <param name="client">WebServerClient used to ask for access token and resources.</param>
        /// <param name="state">AuthorizationState containing the access token.</param>
        /// <param name="path">A path that determines what API endpoint resources are requested from.</param>
        public static T GetProtectedResource <T>(WebServerClient client, IAuthorizationState state, string path)
        {
            // Sets the uri to the api server host provided by Visma gotten from the web.config file
            var apiServerHost = WebConfigurationManager.AppSettings["ApiServerHost"];

            // Creates an api request and and sets method and content type
            var request = (HttpWebRequest)WebRequest.Create(new Uri(String.Format("{0}{1}", apiServerHost, path)));

            request.Method      = "GET";
            request.ContentType = "application/json";

            // Sends the actual request with the authorization state which contains the tokens
            client.AuthorizeRequest(request, state);

            // Gets the request and stores it in webResponse variable
            var webResponse = request.GetResponse();

            // The response is is read into a string and deserialized
            var response = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
            var jss      = new JavaScriptSerializer();
            var result   = jss.Deserialize <T>(response);

            return(result);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Remove this for production it just eats tee  Https invalid certificate error
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

            // usung DotNetOpenAuth lib here but any Auth2 lib should work
            var authServer = new AuthorizationServerDescription
            {
                TokenEndpoint   = new Uri(APIURL + "/oauth/token"),
                ProtocolVersion = ProtocolVersion.V20
            };

            // get our token
            var client = new WebServerClient(authServer, APIURL + "/oauth/token");
            var auth   = client.ExchangeUserCredentialForToken("demo123", "demo123");

            Console.WriteLine(String.Format("Access Token Set to: {0} ", auth.AccessToken));

            // create credit keysale object and transform to json
            var keySale = new { amount = 19.95, credit_card = new { number = "4111111111111111", expiration_month = 12, expiration_year = 20 } };
            var json    = JsonConvert.SerializeObject(keySale);

            Console.WriteLine(Environment.NewLine + string.Format("Json Card String: {0}", json));

            byte[] buf = Encoding.UTF8.GetBytes(json);

            // build request
            var request = (HttpWebRequest)WebRequest.Create(APIURL + "/v1/transactions/sale/keyed");

            request.ContentType   = @"application/json";
            request.Method        = "POST";
            request.ContentLength = buf.Length;

            // add our auth token
            client.AuthorizeRequest(request, auth);
            // buffer the body
            request.GetRequestStream().Write(buf, 0, buf.Length);

            try
            {
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Response: ");
                Console.WriteLine(Environment.NewLine);
                var response = (HttpWebResponse)request.GetResponse();
                using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    Console.WriteLine(reader.ReadToEnd());
                    Console.WriteLine(Environment.NewLine);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine(string.Format("Error: {0}", e.Message));
                Console.WriteLine(Environment.NewLine);
            }
            finally
            {
                Console.WriteLine(Environment.NewLine + "Hit any key to quit: ");
                Console.Read();
            }
        }