cwendt94 / espn-api

ESPN Fantasy API! (Football, Basketball)
MIT License
544 stars 183 forks source link

ESPN Access Denied 2018 Season #278

Open robbie11r1 opened 2 years ago

robbie11r1 commented 2 years ago

Good Morning,

Thank you for your well documented code, this has been great! I am running into one issue that I cannot seem to resolve even after looking through all previous discussions/issues.

I am trying to pull Fantasy Football League data for all seasons 2013-2020 and your "League" class is working great for every season except 2018. I am getting 401 error: "raise ESPNAccessDenied(f"League {league_id} cannot be accessed with swid={cookies.get('espn_s2')} and swid={cookies.get('SWID')}")"

I have verified the season exists on ESPN website, so I am not sure why this specific season is giving this error, and why 2019 and 2020 are having no torouble. It is a private league and I am using SWID and espn_s2 to login to all seasons (except 2018) without an issue.

Any suggestions on how to approach this error?

cwendt94 commented 2 years ago

That is very strange. The account you are using to access the league, was it part of that season 2018? I can't think of any reason why it would give access denied.

zanderman12 commented 2 years ago

Was just coming to see if there was anything on this. I also am getting access denied for a number of public leagues for this same reason. Its always just the 2018 season. My only thought is I know ESPN updated their API that year and so I wonder if it has something to do with that? But same as Robbie, other years work fine.

Overall though thank you so much for your well-documented code!

robbie11r1 commented 2 years ago

@cwendt94 I was actually not part of the 2018 season, I didn't join until 2019. However, the code is working all the way back to 2013, so I am not sure exactly what the issue is.

Sounds like @zanderman12 is experiencing the same issue, so looks to go beyond just my own league. Could it be something with the fact that the API changed that year?

Thank you again!

cwendt94 commented 2 years ago

If you are able to see that season using the ESPN Website you should be able to get that data via their API's. If that is the case, I wonder if there is another token or variable for authentication.

zanderman12 commented 2 years ago

Hey @robbie11r1 and @cwendt94 I have a theory as to what happened. I think with the updated API in 2018, ESPN also increased their security. My theory is that all leagues were set to private in 2018, at least as far as the API is concerned, with the update. Then commissioners have to manually set the league to public after 2018 for the data to be viewable without credentials. I have now had a few leagues where the access was denied in 2018, 2019, and 2020, but was viewable before 2018 without credentials. I have also had a few people send me their espn_s2 and swid cookies and that fixed the issue.

I hope that helps.

Robert-litts commented 6 months ago

@zanderman12 Hey Alexander, finally getting back around to this two years later... better late than never right!

So I am pulling stats from an ESPN league than ran from 2013-2021, and am able to grab stats using this API with my espn_s2 and swid cookies for every year except 2018. This includes 2019, 2020, and 2021, as well as 2013-2017.

So in your example, I'm not exactly experiencing the same issue... it is ONLY the year 2018 that is returning. Unfortunately the commissioner no longer has access to change the league visibility...

Robert-litts commented 6 months ago

So I'm digging into the API a bit and found something odd... I've been doing some manual Get requests and was able to pull my league info for 2018 using what appears to be the "old format":

The below is lines 47-50 from the espn_requests.py; the split for API differences is at 2018:

if year < 2018:
            self.LEAGUE_ENDPOINT += "/leagueHistory/" + str(league_id) + "?seasonId=" + str(year)
        else:
            self.LEAGUE_ENDPOINT += "/seasons/" + str(year) + "/segments/0/leagues/" + str(league_id)

I tried my league with the "older" format, as shown below and it returns proper JSON:

https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/747376?seasonId=2018&view=mTeam
year=2018
league_id=747376

url = f'https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/{league_id}?seasonId={year}'

team_response = requests.get(url, params={"view": "mTeam"})

team_json = team_response.json()[0]

Using the format specified here for 2018 and newer, I am getting an error of "You are not authorized to view this League":

https://fantasy.espn.com/apis/v3/games/ffl/seasons/2018/segments/0/leagues/747376?view=mTeam

@zanderman12 can you take a look at yours and see if you have the same? Maybe the "fix" is as simple as changing line 47 to <= instead of <

Robert-litts commented 6 months ago

Okay, so even more digging and testing and I got this package to run for year 2018, but it breaks every other year :cry:

I suspect it still has to do with the additional [0] key in the JSON data when pulling that year...

For example, in base_league.py lines 48-54 would have to include the [0] key and become:

 def _fetch_teams(self, data, TeamClass, pro_schedule = None):
        '''Fetch teams in league'''
        self.teams = []
        teams = data[0]['teams']
        schedule = data[0]['schedule']
        seasonId = data[0]['seasonId']
        members = data[0].get('members', [])

Not sure if I am off base, but it seems to be what is throwing errors in my testing

Robert-litts commented 6 months ago

Just submitted PR #514 (should also fix duplicate issue #466 as well).

I made the necessary changes to account for 2018 and my tests for years 2013-2021 seem to be working! Also FYI I am the original owner of the account "robbie11r1" who opened this issue... It is an old account of mine that was never used :smile: