R Yahoo Fantasy API - 权限

问题描述 投票:2回答:2

我在使用Yahoo Fantasy API时遇到问题。使用示例herehere,我已经开发了一个非常hacky解决方案来获取我的连接。

首先,我使用以下代码创建一个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联赛ID来获取类似联盟排名的联赛积分。

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。

有什么建议?

r oauth yahoo-api
2个回答
1
投票

我有类似的挣扎,我使用了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


1
投票

不确定你是否还有问题,但我一直在努力解决这个问题,直到找到解决方案。

问题:雅虎已从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")
© www.soinside.com 2019 - 2024. All rights reserved.