whatadewitt / yahoo-fantasy-sports-api

NodeJS wrapper for the Yahoo! Fantasy Sports API
https://yahoo-fantasy-node-docs.vercel.app/
MIT License
200 stars 54 forks source link

Programmatic User Token #36

Closed billderose-zz closed 5 years ago

billderose-zz commented 7 years ago

@whatadewitt Firstly, thanks for putting this library together. It does exactly what I'm looking for. However, I'm having a little trouble getting everything working on a web server. I'd like to write a chat bot that can answer basic questions about the fantasy baseball league I'm in. To do that, it looks like I need an authorization token. What's the best way to get this programmatically? Typically, the user would be promoted with a login page that would re-direct back to the page once authenticated. The "user" in this case will be a virtual machine, but I'd like it to still be able to gain access to my yahoo account. I was hoping this package would take care of making the OAuth call for me. Any ideas?

Note: I'm not talking about application key/secret. I'm specifically referring to the value passed to YahooFantasy.setUserToken.

Thanks in advance.

whatadewitt commented 7 years ago

Hi @billderose,

The unfortunate issue with switching this to use OAuth2 is that Yahoo! doesn't support client tokens, so you're right, it can be a bit of a pain in the ass to get working.

What I did was create a simple single page login with ExpressJS that would authenticate against the Yahoo! server for a given app. Then I stored that user token and refresh token in a Redis store on a box where I run one of my applications from. Then I just connect to the store to get the token whenever I need it, and if it expires I use the refresh token to generate a new token without me having to log in again, and that gets stored. The tokens need to be refreshed after an hour, but this is something I've had running on a box for a while now and works great.

I realize that my language here isn't super clear. I will try to get an example up on GitHub of what I'm doing when I get home this weekend (currently I am at a conference for work in New Orleans, so my time is pretty limited at the moment)

Let me know if this helps however!

--d

billderose-zz commented 7 years ago

Thanks for the quick response @whatadewitt.

I think the issue is that my application attempting to access the Yahoo fantasy sports API is not a web app: it's a chat bot running server side. I'm having trouble wrapping my head around how I can get my service to auth into Yahoo without having to prompt a user for consent. Essentially, I'm looking for some sort of service account or programmatic access to the API that doesn't require a user to enter their login/password everytime it tries to access a resource in Yahoo's API.

Enjoy NOLA!

whatadewitt commented 7 years ago

So here's a somewhat dirty example of an application I built with the API that has the auth...

https://gist.github.com/whatadewitt/dd48d0664b8928be73b65d97ed124731

The builder.js is a simple express server that just renders a login button and when the user logs in it stores a key in Redis.

app.js runs on a cron between 11am and 11pm (every 15 mins) and sends me a push notification if I have players that are in my starting lineup and are benched and vice-versa. You'll see it connects to that same Redis instance to get the token information and if it's expired it will automatically refresh the token and save the new token and then re-send the original request.

Apologies for the lack of comments on it, I just threw this together quickly as a proof of concept and haven't really revisited it as it's worked really well for me thus far :)

Please let me know if you have any questions!

--d