Example #1
0
        public async Task <IActionResult> OnPost()
        {
            IRestClient   client   = new RestClient("http://api.nbp.pl/api");
            IRestRequest  request  = new RestRequest("exchangerates/tables/A?format=json", Method.GET);
            IRestResponse response = client.Get(request);

            Chilkat.JsonObject json = new Chilkat.JsonObject();
            json.Load(response.Content);
            Chilkat.JsonArray jCurrencies = json.ArrayOf("rates");
            int numCurrencies             = jCurrencies.Size;

            for (int i = 0; i < numCurrencies; i++)
            {
                Chilkat.JsonObject empObj = jCurrencies.ObjectAt(i);
                string             tName  = empObj.StringOf("currency");
                string             tCode  = empObj.StringOf("code");
                string             tMid   = empObj.StringOf("mid");
                tMid = tMid.Replace('.', ',');
                decimal  Mid      = Convert.ToDecimal(tMid);
                Currency currency = new Currency(tName, tCode, Mid, DateTime.Now);
                await _db.Currency.AddAsync(currency);

                await _db.SaveChangesAsync();
            }
            return(RedirectToPage("CurrencyList"));
        }
Example #2
0
        public static void ListAllCardholders()
        {
            Chilkat.Rest rest = new Chilkat.Rest();
            bool         success;
            //  URL: https://api.stripe.com/v1/balance
            bool bTls           = true;
            int  port           = 443;
            bool bAutoReconnect = true;

            success = rest.Connect("api.stripe.com", port, bTls, bAutoReconnect);
            if (success != true)
            {
                Log("ConnectFailReason: " + Convert.ToString(rest.ConnectFailReason));
                Log(rest.LastErrorText);
                return;
            }
            Chilkat.StringBuilder sbResponseBody = new Chilkat.StringBuilder();
            success = rest.FullRequestNoBodySb("GET", "/v1/issuing/cardholders", sbResponseBody);
            if (success != true)
            {
                Log(rest.LastErrorText);
                return;
            }
            Chilkat.JsonObject jsonResponse = new Chilkat.JsonObject();
            jsonResponse.LoadSb(sbResponseBody);
            var o        = jsonResponse.StringOf("object");
            var livemode = jsonResponse.BoolOf("livemode");
        }
Example #3
0
        private string getSavedAccessToken()
        {
            if (m_googleAccessJson == null)
            {
                return("");
            }

            Chilkat.JsonObject json = new Chilkat.JsonObject();
            json.Load(m_googleAccessJson);
            return(json.StringOf("access_token"));
        }
Example #4
0
        private void googleDriveListFiles()
        {
            bool success = true;

            Chilkat.AuthGoogle gAuth = new Chilkat.AuthGoogle();
            //  This is our previously obtained access token...
            gAuth.AccessToken = getSavedAccessToken();
            if (gAuth.AccessToken.Length == 0)
            {
                popupError("No previously obtained access token is available.");
                return;
            }

            Chilkat.Rest rest = new Chilkat.Rest();

            //  Connect using TLS.
            bool bAutoReconnect = true;

            success = rest.Connect("www.googleapis.com", 443, true, bAutoReconnect);

            //  Provide the authentication credentials (i.e. the access key)
            rest.SetAuthGoogle(gAuth);

            //  Add a search query parameter to only return files having names matching our criteria
            //  See https://developers.google.com/drive/v3/web/search-parameters
            // ignore = rest.AddQueryParam("q","name contains 'starfish'");
            // ignore = rest.AddQueryParam("maxResults","2");

            //  We are using the Google Drive V3 API... (not V2)
            string jsonResponse = rest.FullRequestNoBody("GET", "/drive/v3/files");

            if (rest.LastMethodSuccess != true)
            {
                fgAppendToErrorLog(rest.LastErrorText);
                popupError("REST request failed.");
                return;
            }

            // A successful JSON response looks like this:
            //{
            //  "kind": "drive#fileList",
            //  "files": [
            //    {
            //      "kind": "drive#file",
            //      "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
            //      "name": "test",
            //      "mimeType": "application/vnd.google-apps.folder"
            //    },
            //    {
            //      "kind": "drive#file",
            //      "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
            //      "name": "starfish4.jpg",
            //      "mimeType": "image/jpeg"
            //    },
            //    {
            //      "kind": "drive#file",
            //      "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
            //      "name": "hamlet2.xml",
            //      "mimeType": "text/xml"
            //    },
            // ...
            //    {
            //      "kind": "drive#file",
            //      "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
            //      "name": "Getting started",
            //      "mimeType": "application/pdf"
            //    }
            //  ]
            //}

            // Iterate over the files and show the name and mimeType of each.
            Chilkat.JsonObject json = new Chilkat.JsonObject();
            json.Load(jsonResponse);
            int numFiles = json.SizeOfArray("files");
            int i        = 0;

            while (i < numFiles)
            {
                json.I = i;
                fgAppendToErrorLog("name: " + json.StringOf("files[i].name") + "\r\n");
                fgAppendToErrorLog("mimeType: " + json.StringOf("files[i].mimeType") + "\r\n");
                i++;
            }
        }
Example #5
0
        // (for Google authorization)
        // Exchange the code for an access token and refresh token.

        // REMEMBER -- this code is running in a background thread because it is called
        // from listenSocket_OnTaskCompleted.
        private bool googleExchangeCodeForToken(string code, string taskUserData)
        {
            // The taskUserData contains JSON information.
            Chilkat.JsonObject taskData = new Chilkat.JsonObject();
            taskData.Load(taskUserData);
            string redirect_uri  = taskData.StringOf("redirect_uri");
            string code_verifier = taskData.StringOf("code_verifier");

            Chilkat.Rest rest = new Chilkat.Rest();

            bool bTls           = true;
            int  port           = 443;
            bool bAutoReconnect = true;
            bool success        = rest.Connect("www.googleapis.com", port, bTls, bAutoReconnect);

            if (success != true)
            {
                fgAppendToErrorLog(rest.LastErrorText);
                return(false);
            }

            success = rest.AddQueryParam("code", code);
            success = rest.AddQueryParam("client_id", googleAppClientId);
            success = rest.AddQueryParam("client_secret", googleAppClientSecret);
            success = rest.AddQueryParam("redirect_uri", redirect_uri);
            success = rest.AddQueryParam("code_verifier", code_verifier);
            success = rest.AddQueryParam("scope", "");
            success = rest.AddQueryParam("grant_type", "authorization_code");

            rest.VerboseLogging = true;
            string responseJson = rest.FullRequestFormUrlEncoded("POST", "/oauth2/v4/token");

            if (rest.LastMethodSuccess != true)
            {
                fgAppendToErrorLog(rest.LastErrorText);
                return(false);
            }

            //  When successful, the response status code will equal 200.
            if (rest.ResponseStatusCode != 200)
            {
                //  Examine the request/response to see what happened.
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("LastErrorText: " + rest.LastErrorText);
                sb.AppendLine("response status code = " + Convert.ToString(rest.ResponseStatusCode));
                sb.AppendLine("response status text = " + rest.ResponseStatusText);
                sb.AppendLine("response header: " + rest.ResponseHeader);
                sb.AppendLine("response body (if any): " + responseJson);
                sb.AppendLine("---");
                sb.AppendLine("LastRequestStartLine: " + rest.LastRequestStartLine);
                sb.AppendLine("LastRequestHeader: " + rest.LastRequestHeader);
                fgAppendToErrorLog(sb.ToString());
                return(false);
            }

            // A successful response JSON will look like this:
            //{
            // "access_token": "ya29.Ci9ZA-Z0Q7vtnch8xxxxxxxxxxxxxxgDVOOV97-IBvTt958xxxxxx1sasw",
            // "token_type": "Bearer",
            // "expires_in": 3600,
            // "refresh_token": "1/fYjEVR-3Oq9xxxxxxxxxxxxxxLzPtlNOeQ"
            //}
            m_googleAccessJson = responseJson;

            fgAppendToErrorLog(responseJson);

            return(true);
        }