R Yahoo Fantasy API - 许可

R Yahoo Fantasy API - Permission

我在使用 Yahoo Fantasy API 时遇到问题。使用示例 here and here,我开发了一个非常老套的解决方案来设置我的连接。

首先,我使用以下代码创建一个 oauth1 令牌:

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)

然后我有另一段代码可以让我签名:

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)

在我看来,我应该只需要其中之一即可访问 API,但我不能只使用其中一个。

无论如何,这确实让我能够建立连接。为了访问 API,我需要游戏 ID。根据上面链接的演练之一中的说明,我使用此代码来执行此操作:

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"]]

game_key 最终成为 378。因此,我应该能够使用我刚刚找到的游戏密钥以及我的联盟唯一的联盟 ID 94107 来访问我的联盟的联盟排名之类的东西。

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")

但是当我将它打印到屏幕上时,我得到:

> 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/>

回复:You are not allowed to view this page because you are not in this league 是我挂在这里的原因。我正在使用相同的 Yahoo 登录名来创建我用来建立梦幻团队的 API。

有什么建议吗?

不确定您是否还有问题,但我为此苦苦挣扎了一段时间,直到找到解决方案。

问题:Yahoo 已从 OAuth1.0 切换到 OAuth2.0。这意味着您在网上找到的许多示例脚本 -- 几乎所有脚本都是在此更改之前创建的-- 不再起作用。

在您提供的示例代码中,看起来 1.0 和 2.0 都在使用(这里有一个有趣的注意事项:1.0 功能用于创建 "sig" 变量 -- 签名令牌 -- 在 2.0 中不再需要)。

这是一个重写,应该可以在没有烦人的授权问题的情况下完成您想要做的事情:

library(httr)

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

b_url <- "https://fantasysports.yahooapis.com" #base url

#Create Endpoint
yahoo <- httr::oauth_endpoint(authorize = "https://api.login.yahoo.com/oauth2/request_auth"
                              , access = "https://api.login.yahoo.com/oauth2/get_token"
                              , base_url = b_url)
#Create App
myapp <- httr::oauth_app("yahoo", key=cKey, secret = cSecret,redirect_uri = "oob")

#Open Browser to Authorization Code
httr::BROWSE(httr::oauth2.0_authorize_url(yahoo, myapp, scope="fspt-r"
                                          , redirect_uri = yahoo_app$redirect_uri))

#Create Token
yahoo_token <- httr::oauth2.0_access_token(yahoo,yahoo_app,code="[ENTER CODE FROM BROWSER HERE]")
save(yahoo_token,file="yahoo_token.Rdata")

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

baseURL     <- "https://fantasysports.yahooapis.com/fantasy/v2/league/"
standingsURL <- paste(baseURL, leagueKey, "/standings")
standings_page <- GET(standingsURL,
                      add_headers(Authorization=paste0("Bearer ", yahoo_token$access_token)))

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

我也遇到过类似的问题,我使用了 Birchman 的答案并进行了大量的反复试验。

我是这样解决的。

从雅虎输入密钥和密码后,您可以执行以下操作。当然,我没有展示我的。

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")

现在,当您执行下一部分时,您将弹出一个浏览器。您必须复制并粘贴提供的代码。

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)

下一节将帮助您构建 url 以获得所需的数据。

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

现在,下一段代码将提取您要查找的数据。

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))

更多细节,这里是Fantasy Football Blog Post我写的