question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

401 (Unauthorized) error on Azure

See original GitHub issue

I am getting the following error on Azure: Flurl.Http.FlurlHttpException: Request to https://query1.finance.yahoo.com/v7/finance/download/MCD?period1=1436745600&period2=1499817600&interval=1d&events=history&crumb= failed with status code 401 (Unauthorized). at Flurl.Http.Configuration.FlurlMessageHandler.d__1.MoveNext() in C:\projects\flurl\src\Flurl.Http.Shared\Configuration\FlurlMessageHandler.cs:line 59 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClient.d__58.MoveNext()

It works flawlessly for a day or two with requests made every few hours, then the above exception starts being thrown. It looks like the crumb parameter is empty? What could cause that?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pljanotcommented, Jul 14, 2017

Hello guys, I will just share some of my slim version of flurlClient with repeat logic for generic FlurlHttpException. Maybe you will find it usefull:

` public class YahooFlurlClient { private static string _crumb;

    private const string QueryUrl = "https://query1.finance.yahoo.com/v7/finance/download";
    private const string CrumbUrl = "https://query1.finance.yahoo.com/v1/test/getcrumb";

    private const string Period1Tag = "period1";
    private const string Period2Tag = "period2";
    private const string IntervalTag = "interval";
    private const string EventsTag = "events";
    private const string CrumbTag = "crumb";

    private static readonly IDictionary<Period, string> PeriodMap = new Dictionary<Period, string>
    {
        {Period.Daily, "d"},
        {Period.Weekly, "w"},
        {Period.Monthly, "m"}
    };

    public static async Task<Stream> GetResponseStreamAsync(string symbol, DateTime? startTime, DateTime? endTime,
        Period period = Period.Daily, string events= "history")
    {
        var yahooClient = YahooClientFactory.GetYahooClient;

        //Example URI
        //https://query1.finance.yahoo.com/v7/finance/download/
        //SBUX?period1=1451602800&period2=1496140683&interval=1d&events=history&crumb=.BprRZODzhT
        var url = QueryUrl
            .AppendPathSegment(symbol)
            .SetQueryParam(Period1Tag, (startTime ?? new DateTime(1970, 1, 1)).ToUnixTimestamp())
            .SetQueryParam(Period2Tag, (endTime ?? DateTime.Now).ToUnixTimestamp())
            .SetQueryParam(IntervalTag, $"1{PeriodMap[period]}")
            .SetQueryParam(EventsTag, events)
            .SetQueryParam(CrumbTag, YahooClientFactory.Crumb);

        try
        {
            return await yahooClient.EnableCookies()
                .WithUrl(url)
                .GetAsync(CancellationToken.None)
                .ReceiveStream();
        }
        catch (Exception ex) when (!(ex is FlurlHttpException))
        {
            return await yahooClient.EnableCookies()
                .WithUrl(url)
                .GetAsync(CancellationToken.None)
                .ReceiveStream();
        }
    }
}

public static class YahooClientFactory
{
    public static IFlurlClient GetYahooClient => yahooClientInstance;
    public static string Crumb { get; }

    private static readonly IFlurlClient yahooClientInstance;
    private const string CookieUrl = "https://finance.yahoo.com";

    private static string userAgent = "User-Agent";
    private static string headerString =
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
    private const string CrumbUrl = "https://query1.finance.yahoo.com/v1/test/getcrumb";

    static YahooClientFactory()
    {
        yahooClientInstance =
            new FlurlClient()
                .WithHeader(userAgent, headerString)
                .EnableCookies();

        yahooClientInstance.WithUrl(CookieUrl)
            .GetAsync(CancellationToken.None)
            .Result
            .EnsureSuccessStatusCode();

        Crumb = yahooClientInstance
            .WithUrl(CrumbUrl)
            .GetAsync(CancellationToken.None)
            .ReceiveString()
            .Result;
    }
}

public enum Period
{
    Daily,
    Weekly,
    Monthly
}

public static class UtilsExtension
{

    private static readonly DateTime DefaultUnixDateTime = new DateTime(1970, 1, 1);

    public static string Name<T>(this T @enum)
    {
        string name = @enum.ToString();
        if (typeof(T).GetMember(name).First().GetCustomAttribute(typeof(EnumMemberAttribute))
                is EnumMemberAttribute attr && attr.IsValueSetExplicitly)
            name = attr.Value;
        return name;
    }

    public static T GetValue<T>(this string str)
        => (T)Convert.ChangeType(str, typeof(T));

    public static string ToUnixTimestamp(this DateTime dateTime)
        => ((DateTimeOffset)dateTime).ToUnixTimeSeconds().ToString();

}

} `

0reactions
mayerwincommented, Jul 14, 2017

Thanks a lot! It seems to work much better already, I’ll let you know if I still encounter issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving unauthorized errors (401) while invoking APIs ...
"statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }.
Read more >
Azure AD API request 401 Unauthorized
Azure throws a clear error if you attempt to call the API with a scope that does not exists or such, but in...
Read more >
App Registration - The remote server returned an error
I have registered an app in Azure and using the service principal to authenticate to the app, and get a listing of all...
Read more >
401 Unauthorized Error–Azure Active Directory (AD)
Solution 1: Ensure Resource Parameter ; Solution 2: Ensure App ID URI mentioned in the Service ; Solution 3: Delete & Create new...
Read more >
Getting 401 Unauthorized for Post requests using Azure ...
If it is more than 30 seconds, you will get a 401 error. Check/calculate the content length in the request and omit any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found