简体   繁体   中英

C# API call from MarketStack and print values - Error 403

I would like to make a successful API call, then print the values in order to see if it works. My main goal is to analyze the data, after I can make a successful API call, and build a systematic strategy for trading.

System.Net.Http.HttpRequestException: "Response status code does not indicate success: 403 (Forbidden)

namespace marketstacktest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
         
            var options = Options.Create(new MarketstackOptions() { ApiToken = "secretTokenHere" });
            var marketstackService = new MarketstackService(options, NullLogger<MarketstackService>.Instance);

            var appleSymbol = "AAPL";
            var fromDate = DateTime.Now.AddDays(-200);
            var toDate = DateTime.Now;

            //error at the await  System.Net.Http.HttpRequestException: "Response status code does not indicate success: 403 (Forbidden)."

            List<Marketstack.Entities.Stocks.StockBar> stock = await marketstackService.GetStockEodBars(appleSymbol, fromDate, toDate);
            
            foreach (var stock_i in stock)
            {
                Console.WriteLine($"close: {stock_i.Close}");
            }
        }
    }
}

In the API manual, which is directly linked from the github, it gives information about all of the error codes. The relevant ones here are these two:

Code Type Description
403 https_access_restricted HTTPS access is not supported on the current subscription plan.
403 function_access_restricted The given API endpoint is not supported on the current subscription plan.

Their class library on github is just wrapping a json REST api. Every call to the API is just an http request, returning data as json objects. The 403 error indicates that your request was accepted as a valid request, but intentionally rejected by the server for some reason. And according to the docs, the error was because your account is not allowed access to either https or to the type of request.

Their free-tier subscription only includes end-of-day data, which is what you requested, so it wouldn't make sense for that not to be allowed. So, your app is almost certainly making an https call.

I went to the examples at the very beginning of their quick start guide, and chose the end-of-day example to match your app, and clicked on the link. It worked, and gave a bunch of json records. But, the request they made was using 'http' not 'https'.

Changing the requst to 'https' elicited a 403 response with this content (formatted for readability):

{
    "error":
    {
        "code": "https_access_restricted",
        "message": "Access Restricted - Your current Subscription Plan does not support HTTPS Encryption."
    }
}

At this point we have enough to be almost certain that this is your issue. The final thing is to go look up how to turn https requests off in their class library. To avoid having to go through the code, I checked the help at the bottom of the page one more time, and found this (formatted for readability):

    var options = Options.Create(new MarketstackOptions(){
        ApiToken = apiKey,
        MaxRequestsPerSecond = 3,
        Https = true 
    });

Welp. This should probably be in their first example, since that's what people are most likely to try first, but it's not. So, to stop trying to make http requests, you just need to set the Https option to false in your code. You just need to add that to the options in your code, like so:

    var options = Options.Create(new MarketstackOptions(){
        ApiToken = "secretTokenHere",
        Https = false
    });

I will leave the testing to you, but from the browser test, we know that the request should work, unless there's a bug in their library. Given the information that was available, this is almost certainly the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM