adam-ducker / mlbv

Command-line interface MLB game information. Game schedule, scores, and standings
GNU General Public License v3.0
18 stars 5 forks source link

Error refreshing access token #8

Open enjoymoreradio opened 1 month ago

enjoymoreradio commented 1 month ago
Traceback (most recent call last):
  File "/home/enjoymoreradio/.local/bin/mlbv", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/enjoymoreradio/.local/lib/python3.12/site-packages/mlbv/mlbam/mlbv.py", line 510, in main
    return mlbstream.play_stream(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/enjoymoreradio/.local/lib/python3.12/site-packages/mlbv/mlbam/mlbstream.py", line 140, in play_stream
    stream_url = mlb_session.lookup_stream_url(game_rec["game_pk"], media_playback_id)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/enjoymoreradio/.local/lib/python3.12/site-packages/mlbv/mlbam/mlbsession.py", line 319, in lookup_stream_url
    "Authorization": self.access_token,
                     ^^^^^^^^^^^^^^^^^
  File "/home/enjoymoreradio/.local/lib/python3.12/site-packages/mlbv/mlbam/common/session.py", line 128, in access_token
    self._refresh_access_token()
  File "/home/enjoymoreradio/.local/lib/python3.12/site-packages/mlbv/mlbam/mlbsession.py", line 264, in _refresh_access_token
    device_id = session_response["device"]["id"]
                ~~~~~~~~~~~~~~~~^^^^^^^^^^
KeyError: 'device'

The response I get when requesting token is:

{'error': 'invalid_grant', 'error_description': 'The authorization code is invalid or has expired.'}
adam-ducker commented 1 month ago

Over the All Star break MLB implemented a newer Okta Interaction Code grant type, replacing the old auth system. I'm still trying to fix my private app that runs in Ruby on Rails and when I do I'll try and give guidance to some of the Python developers and hopefully somebody can implement that solution in mlbv. Until then unfortunately the Ruby on Rails app and the Python app are both dead in the water.

More info here if you care: https://developer.okta.com/docs/concepts/interaction-code/

enjoymoreradio commented 1 month ago

Thanks for the quick response. Authentication protocols are more complicated than my novice python skills can handle, but I'll read the docs and see what I can piece together.

adam-ducker commented 1 month ago

It's not just the auth either it turns out. It looks like MLB.tv switched entirely to a new streaming service with Google. The master m3u8 packages are completely different now. It will take pretty significant changes to mlbv to restore service. I'll add more details if I ever get my Ruby on Rails app working.

kidshare commented 1 month ago

I managed to download yesterday's Padres game using youtube-dlp by accidentally stumbling across "X-CDN-Token" in the request headers of a *.vtt file. I passed that value as a header and used the master.m3u8 for the URL. Hope this helps you fix this. Manual downloads are OK but I'd much rather do it this way!

enjoymoreradio commented 1 month ago

The proof of concept posted by @mattiasa link worked this way, provided you have a valid okta access token

mattiasa commented 1 month ago

It seems the only thing that's needed to get media download to work at least is to get a device_id from the graphql endpoint instead of the old method. I've implemented a hack to do that that along with getting the media url from the same endpoint.

It's available in https://github.com/mattiasa/mlbv/tree/new-auth

This is by no means ready to be merged but feel free to use it. It was enough to allow me to download and watch last night's Cubs vs Diamondbacks game.

adam-ducker commented 1 month ago

That would be good. In my own app I've implemented the Interaction Code grant type stuff and was able to rip out all the entitlement token stuff. The app works locally but not on the web due to CORs issues. It's still a work in progress. If I ever get it all working we can compare notes and hopefully the python app will be ready to go again.

adam-ducker commented 1 month ago

You may run into a problem where you need to use the interaction code logic so let me know and I can show you my ruby code that will give you a sense of what you need to do. I've resolved the CORs problem with the streaming files in my app and updated everything to work with the new content id it uses instead of the older media id. I had to use broadcasts data instead of the older epg stuff but I'm not sure if you're running into that problem or not.