Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Exception: could not authenticate: {authz_contet} #70

Open
SlimJimPoisson opened this issue Mar 3, 2023 · 11 comments
Open

Exception: could not authenticate: {authz_contet} #70

SlimJimPoisson opened this issue Mar 3, 2023 · 11 comments

Comments

@SlimJimPoisson
Copy link

This error started a day or so ago. Until then everything was working perfectly. Now I consistently get the following error:

File "c:\program files\python37\lib\site-packages\mlbv\mlbam\mlbsession.py", line 183, in get_okta_token
raise Exception("could not authenticate: {authz_contet}")
Exception: could not authenticate: {authz_contet}

I could include the rest of the trace stack if needed. I have seen that this issues was raised in the streamglob hub (https://github.com/tonycpsu/streamglob/issues), but the user indicated that the problem magically disappeared so there was no solution as such.

If anyone has any ideas I would like to begin downloading baseball again.

@tonywagner
Copy link

That is just a generic authentication error. In this case, I think it is because the authentication type changed from token to “code” which will require some updates to handle.

@SlimJimPoisson
Copy link
Author

Thanks, Tony. Glad to see you're still around! Let me know if I can assist. This one is pretty deep in your core code and I knew I wasn't going to be able to fix it even if I understood why it failed.

I'm guessing the issue is in obtaining the initial URL to begin the stream, but I seem to remember there is a need to keep authenticating even after that.

Is there is any hack-workaround to get a downloadable stream going or will it require learning their new dance?

@kmac
Copy link
Owner

kmac commented Mar 4, 2023

I'm completely locked out of mlb.tv subscriptions for watching my team (Blue Jays are blacked out across Canada), so I'm pretty much done with this project. I'll likely be archiving this repo soon... If anyone want's to take it over that would be great.

@kidshare
Copy link

kidshare commented Mar 4, 2023

Is there is any hack-workaround to get a downloadable stream going or will it require learning their new dance?

I downloaded the Padres-Cubs using streamlink. It takes some elbow grease using dev tools (F12)

Open the stream you want and hit F12 to open dev tools. You might have to reload the stream if it starts playing before you hit F12. Also open notepad to paste these things and then copy the entire string into a command line window.

You want to retrieve two strings from dev tools, the authorization key (type "keys" into the search box) and the m3u link (type "m3u" into the search box)

For the authorization key, click on the XHR file and scroll down on the right to the authorization key, a very long string of jumbled letters and numbers. Right click on that to copy and paste it into the code listed below. Then find the m3u link you want, right click>copy and paste that into the code below.

These will all go on one line in the console window:

streamlink -o "2023-03-03-SD-CHC.ts"
--http-header "authorization=very long authorization key"
"long m3u link"
best

If the stream is live already and you want to back up to the beginning to download, insert --hls-start-offset HH:MM:SS or --hls-live-restart

Also make sure you CD to the directory you want to download into before you start streamlink
You can choose a lesser quality for viewing on a phone instead of "best," the choices are 180p (worst), 216p, 288p, 360p, 504p, 540p, 720p_alt, 720p (best)

@ColbyRasmus
Copy link

kidshare, those are some great steps you've provided us, and I decided to follow what you've set out. One snag, maybe you can help? I keep getting "Could not open stream" errors. Maybe it has something to do with the m3u file I'm choosing. Which would you suggest be the one I'd want? There seem to be several m3u links that appear almost identical.

@kidshare
Copy link

kidshare commented Mar 4, 2023

kidshare, those are some great steps you've provided us, and I decided to follow what you've set out. One snag, maybe you can help? I keep getting "Could not open stream" errors. Maybe it has something to do with the m3u file I'm choosing. Which would you suggest be the one I'd want? There seem to be several m3u links that appear almost identical.

I used the stream that starts with "master." Maybe you started a little early? I got the same error message till just before the Padres game even though I could see the "Event will start shortly" screen. Doing it this way seems to skip commercials so I think you have to wait for the actual game stream to start.

@adam-ducker
Copy link

adam-ducker commented Mar 5, 2023

The comment above is correct. The MLB oauth2 authorize endpoint that takes a STATE and NONCE and SESSION TOKEN now no longer returns "data.access_token" as it used to. It returns "data.code" which needs to be used in an additional step now.

I have a PHP app that was built using this python app as an example so when it broke today I thought about checking to see if mlbv was working and it seems to be broken in the same way. I don't know python very well but here is the PHP code I'm using now that is working again that should be enough to show you how to fix mlbv.

We need two strings now, a code_verifier and a code_challenge. I generate a code_verifier just like I generate my state and nonce, a random 58 character URL safe string. I generate the code_challenge by base 64 encoding a sha256 hashed version of the code_verifier using an example from this site: https://www.oauth.com/oauth2-servers/pkce/authorization-request/

  $codeVerifier = getRandomString(58);
  $codeChallenge = rtrim(strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '=');

Example code_verifier: 7dfa3871146d1b2a002b59926458c7b098def154cd6
Example correct code_challenge: Lif_WlZK5OdoSN5vyKYfIzt6CsASotDq2VKtR1U7xOk

  $url = "https://ids.mlb.com/oauth2/aus1m088yK07noBfh356/v1/authorize";
  
  $params = array(
    "client_id" => $clientId
    "redirect_uri" => "https://www.mlb.com/login",
    "response_type" => "code", // <--- change #1, this needs to be 'code' instead of 'id_token token'
    "response_mode" => "okta_post_message",
    "prompt" => "none",
    "state" => $state,
    "nonce" => $nonce,
    "code_challenge" => $codeChallenge, // <--- change #2, here we put our code_challenge
    "code_challenge_method" => "S256", // <--- change #3, here we set the challenge method, always S256
    "sessionToken" => $sessionToken,
    "scope" => "openid email"
  );

This returns a "data.code" value we'll use in the next endpoint.

  $url = "https://ids.mlb.com/oauth2/aus1m088yK07noBfh356/v1/token";
  
  // here we use the code we got from the previous endpoint, and the code_verifier value
  $paramsStr = 
    "client_id=$clientId" .
    "&redirect_uri=https://www.mlb.com/login" .
    "&grant_type=authorization_code" .
    "&code_verifier=$codeVerifier" . 
    "&code=$code";
  
  $headers = array(
    "accept: application/json",
    "content-type: application/x-www-form-urlencoded"
  );

  $options = array(
    'http' => array(
      'method'  => 'POST',
      'content' =>  $paramsStr,
      'header'=> implode("\r\n", $headers) . "\r\n"
    )
  );

This will return a JSON response containing the access_token and the rest of the app remains the same.

Hopefully this helps.

@adam-ducker
Copy link

I may attempt to create a PR if I can get mlbv running locally again but I can't make any promises.

@adam-ducker
Copy link

Alright, I submitted a PR for consideration.

@kidshare
Copy link

kidshare commented Mar 5, 2023

File "c:\computer\python395\lib\site-packages\mlbv\mlbam\mlbsession.py", line 76, in init
session.Session.init(self, USER_AGENT, PLATFORM)
TypeError: init() missing 1 required positional argument: 'platform'

@elle-the-dev
Copy link

elle-the-dev commented Mar 5, 2023

If you want to use the patched version before it's merged, you'll need to clone the repo, checkout the patch branch, and install it from the project root directory.

git clone https://github.com/adam-ducker/mlbv
cd mlbv
git checkout use-pkce
pip install .

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants