Search code examples
roauthyahoo-api

R Yahoo Fantasy API - Permission


I am having issues using the Yahoo Fantasy API. Using examples here and here, I have developed a pretty hacky solution to get my connection set.

First, I create an oauth1 token with the following code:

library(httr)

cKey <- "mykey"
cSecret <- "mysecret"

oauth_endpoints("yahoo")
myapp <- oauth_app("yahoo", key = cKey, secret = cSecret)
token <- oauth1.0_token(oauth_endpoints("yahoo"), myapp)

Then I have another piece of code that gets me a signature:

yahoo    <-oauth_endpoints("yahoo")
myapp <- oauth_app("yahoo", key=cKey, secret=cSecret)
yahoo_token<- oauth2.0_token(yahoo, myapp, cache=T, use_oob = T)
sig <- sign_oauth1.0(myapp, yahoo_token$oauth_token, yahoo_token$oauth_token_secret)

It seems to me like I should only need one of these to access the API, but I can't make it work with just one.

Anyways, this does allow me to get my connection set up. In order to access the API, I need the game ID. Per instructions in one of the above-linked walkthroughs, I use this code to do that:

page_mlb <-GET("http://fantasysports.yahooapis.com/fantasy/v2/game/mlb?format=json", sig)
page_mlb_parse <- content(page_mlb, as="parsed", encoding="utf-8")
game_key <- page_mlb_parse[["fantasy_content"]][["game"]][[1]][["game_key"]]

That game_key ends up being 378. Thus, I should be able to access something like the league standings for my league using the game key I just found along with my league ID of 94107 which is unique to my league.

leagueKey <- paste0(game_key,'.l.',lg_id)

baseURL     <- "http://fantasysports.yahooapis.com/fantasy/v2/league/"
standingsURL<-paste(baseURL, leagueKey, "/standings", sep="")
standings_page <- GET(standingsURL,sig)

standings_parse <- content(standings_page, as = "parsed", encoding = "utf-8")

But when I print that to the screen, I get:

> standings_parse
{xml_document}
<error lang="en-us" uri="http://fantasysports.yahooapis.com/fantasy/v2/league/378.l.94107/standings" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns="http://www.yahooapis.com/v1/base.rng">
[1] <description>You are not allowed to view this page because you are not in this league.</description>
[2] <detail/>

The response: You are not allowed to view this page because you are not in this league is what has me hung up here. I am using the same Yahoo login to create the API that I used to set up my fantasy team.

Any suggestions?


Solution

  • I was having similar struggles and I used Birchman's answer and a lot of trial and error.

    Here's how I solved it.

    Once you key and secret code from yahoo, you can do the following. Of course, I've not shown mine.

    options("httr_oob_default" = T)
    
    cKey     <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    cSecret  <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    yahoo <- httr::oauth_endpoint(authorize ="https://api.login.yahoo.com/oauth2/request_auth", access = "https://api.login.yahoo.com/oauth2/get_token", base_url = "https://fantasysports.yahooapis.com")
    
    myapp <- httr::oauth_app("yahoo", key=cKey, secret = cSecret,redirect_uri = "oob")
    

    Now, when you do the next section, you'll have a browser pop up. You'll have to copy and paste the code provided.

    httr::BROWSE(httr::oauth2.0_authorize_url(yahoo, myapp, scope="fspt-r", redirect_uri = myapp$redirect_uri))
    
    passcode = "xxxxxxx"
    
    yahoo_token <- httr::oauth2.0_access_token(yahoo,myapp,code=passcode)
    

    The next section helps you build the url to get the data you need.

    standings_page <- GET("https://fantasysports.yahooapis.com/fantasy/v2/game/nfl", add_headers(Authorization=paste0("Bearer ", yahoo_token$access_token)))
    XMLstandings<- content(standings_page, as="parsed", encoding="utf-8")
    doc<-xmlTreeParse(XMLstandings, useInternal=TRUE)
    myList<- xmlToList(xmlRoot(doc))
    game_key = myList$game$game_key
    game_key
    

    Now, the next chunk of code will extract the data you are looking for.

    baseURL <- "https://fantasysports.yahooapis.com/fantasy/v2/league/"
    
    leagueID <- "1244633"
    
    tag <- "/scoreboard;week=1"
    
    standingsURL <-paste0(baseURL,game_key,".l.",leagueID,tag)
    standings_page <- GET(standingsURL, add_headers(Authorization = 
    paste0("Bearer ", yahoo_token$access_token)))
    XMLstandings <- content(standings_page, as = "parsed", encoding = "utf-8")
    
    doc <- xmlTreeParse(XMLstandings, useInternal = TRUE)
    myList <- xmlToList(xmlRoot(doc))
    

    For more details, here is a Fantasy Football Blog Post I wrote.