mattdodge / yahoofantasy

A Python SDK for the Yahoo! Fantasy Sports API
65 stars 18 forks source link

Cannot determine access code from URL string when running yahoofantasy login CLI #4

Closed acedriven closed 3 years ago

acedriven commented 3 years ago

I'm not entirely sure this is an issue w/ yahoofantasy or some inner workings of my versions of various python libraries. But, I wanted to add/ask here as I'm wondering if this is something y'all have run into?

Here's what I'm running into (and I have a lot of additional output from http.server.BaseHTTPRequestHandler to share as it makes sense):

~:1 yahoofantasy login
Launching browser for Yahoo authentication. If this doesn't work use this link manually:
https://api.login.yahoo.com/oauth2/request_auth?client_id=<code>&redirect_uri=https%3A%2F%2Flocalhost%3A8000&response_type=code
Launching OAuth handler server on localhost:8000
ERROR: Couldn't determine access code

This results in image

When I comment out login.Handler.log_message(), I end up seeing this rather gnarly output/error code:

~:1 yahoofantasy login
Launching browser for Yahoo authentication. If this doesn't work use this link manually:
https://api.login.yahoo.com/oauth2/request_auth?client_id=<code>&redirect_uri=https%3A%2F%2Flocalhost%3A8000&response_type=code
Launching OAuth handler server on localhost:8000
127.0.0.1 - - [16/Oct/2020 12:40:47] code 400, message Bad request version ('localhost\x00\x17\x00\x00ÿ\x01\x00\x01\x00\x00')
127.0.0.1 - - [16/Oct/2020 12:40:47] "ü$Ë©Åx'HKÁ¦î(Z~(bËÌã¨ÖéVÊ©= îçUzÇ!÷znf̦úZÌã{£È?úÛCY zzÀ+À/À,À0̨̩ÀÀ/5
                                                localhostÿ" 400 -
ERROR: Couldn't determine access code from URL string
mattdodge commented 3 years ago

It looks like your redirect URI is using https (https://localhost:8000). The server is likely just running regular HTTP though.

To test, see if you can go to http://localhost:8000 and at least see something.

Not sure why it's defaulting to the https URL but you can override it like so

yahoofantasy login --redirect-uri http://localhost:8000
acedriven commented 3 years ago

Hmm - when I do that I end up on this Yahoo! page:

image

FWIW, my Yahoo! app has a redirect URI of https://localhost:8000, and Yahoo! won't let me change to http://localhost:8000

mattdodge commented 3 years ago

Ugh you're right, it looks like Yahoo is requiring HTTPS for all of their redirect URIs now. This really sucks for local development and testing. I'll reach out and see if there's a way around this. If you have an existing app with an HTTP redirect you should be fine.

As a workaround in the meantime you can use something like ngrok to generate a secure tunnel. Fire up ngrok and point it to your localhost port 8000. Then use the ngrok HTTPS URL as your Yahoo app redirect URI. Then try to run yahoofantasy login --redirect-uri <ngrok https uri>.

acedriven commented 3 years ago

Interesting - I'm not sure I fully understand what's happening here, but I'll check out ngrok!

Is this something you/we can even fix w/in yahoofantasy, e.g. by updating login.Handler() or something?

mattdodge commented 3 years ago

I can launch the server using HTTPS but getting a trusted certificate is going to be the tricky part. If the cert isn't trusted by your machine you'll get the Chrome warning saying "this site is not trusted." Although I think you should be able to click through and bypass the warning and have it work, so while it's not great at least it doesn't require setting up ngrok.

acedriven commented 3 years ago

Gotcha - I'll leave this decision up to you (I'm not even sure if doing this would make it so that existing apps w/ HTTP would no longer work)!

mattdodge commented 3 years ago

@acedriven thanks for bringing this up. I switched the logins server over to be https by default now. You'll see the Chrome warning about the site being unsecure but just skip through that, it should all work.

Give it a try by upgrading pip install -U yahoofantasy. It may take a few mins to get through the PyPI cache, make sure you're on version 1.1.1. Let me know if you're still having any issues

acedriven commented 3 years ago

Amazing - just tried and this worked. Thanks for the super fast debug + fix! 😄