AdrianJSClark / aydsko-iracingdata

A .NET access library for querying the iRacing "Data" API.
MIT License
28 stars 8 forks source link

Re-Authenticate? #217

Closed Robertsmania closed 1 month ago

Robertsmania commented 1 month ago

I'm often seeing calls to this endpoint fail: var raceGuideData = await _dataClient.GetRaceGuideAsync();

When it fails, the error message back is usually that its unauthorized.

When my class starts up it follows an initialization and authorization sequence similar to whats presented in the read.me:

        private static ServiceCollection _services = new ServiceCollection();
        private static IDataClient _dataClient;
...
                    _services.AddIRacingDataApi(options =>
                    {
                        options.UserAgentProductName = "Robertsmania";
                        options.UserAgentProductVersion = new Version(1, 0);
                    });

                    using var serviceProvider = _services.BuildServiceProvider();
                    _dataClient = serviceProvider.GetRequiredService<IDataClient>();
                    _dataClient.UseUsernameAndPassword(iRacingUserName, iRacingPassword);

                    var infoResponse = await _dataClient.GetMyInfoAsync();
                    _vaProxy.WriteToLog($"Driver name: {infoResponse.Data.DisplayName}", "grey");
                    _vaProxy.WriteToLog($"Customer ID: {infoResponse.Data.CustomerId}", "grey");
                    _vaProxy.WriteToLog($"Club: {infoResponse.Data.ClubName}", "grey");
                    _vaProxy.WriteToLog("iRacingData Logged in...", "pink");

I usually watch the output of my app when its starting up and verify that the VoiceAttack log shows the driver info, which shows that it was authenticated properly then.

Later on (often an hour or so later when I've finished a race) when I want to get the schedule info, these days it often seems to fail with the authentication error when I call:

var raceGuideData = await _dataClient.GetRaceGuideAsync();

Is there a way I can attempt to re-login at that point? I may be confused about how its working, but it seems like the _dataClient already has my credentials and hitting the GetMyInfoAsync or other end point is what actually loggs the client in. So when it fails on a subsequent call later on, I dont see how to get it to try and re-authenticate...

And yes, I do appreciate the the instability/issues are most likely on the iRacing /data API side and not a shortcoming of aydsko-iracingdata itself. But I'd love to be able to get it to dynamically retry when it fails rather than having to restart the application.

Robertsmania commented 1 month ago

I’ve been able to get it to work by setting the data client to null and re-initializing and authenticating when it gives me the error. I’m only trying again once so if the API really is hosed it doesn’t snag other stuff, bit seems sufficient to log back in if it just fell out of authorization.

AdrianJSClark commented 1 month ago

Hey there! Your issues are probably related to #213

Unfortunately I've been having a super-stressful time at work and haven't had a chance to look into it yet.