一整天都在搞这个。我只是想从Strava那里提取数据来操纵R,但我搞不懂。我不是程序员,但我做过一次连接到github文件的工作,但是事情并没有按照我的方式进行。
这是一个基本的错误,还是我遗漏了一些我还不明白的更深层次的原则?
当我试图运行我的代码时,我得到了这样的结果:
x <- pullStravaData();
> x
<Token>
<oauth_endpoint>
request: https://www.strava.com/oauth/authorize
authorize: http://localhost:1410
access: https://www.strava.com/oauth/token
client_id: 3979
response_type: code
<oauth_app> strava
key: XX # I hid this, not sure if it matters
secret: <hidden>
<credentials> message, errors这是我的密码:
pullStravaData <- function() {
# because I think this is important
library(httr);
# not sure if this is relevant
responseType = "code";
clientId = 3979;
# create app
clientSecret = trust_that_i_entered_this;
accessToken = and_this_wrapped_in_quotes;
myapp <- oauth_app("strava", accessToken, clientSecret);
# get oauth credentials
request <- "https://www.strava.com/oauth/authorize";
authorize <- "http://localhost:1410";
access <- "https://www.strava.com/oauth/token";
strava_token <- oauth2.0_token(
oauth_endpoint(request, authorize, access),
myapp);
data <- stravaPost(strava_token, clientId, clientSecret);
}
stravaPost <- function(token, clientId, clientSecret) {
# I saw this happen somewhere else, but I'm pretty sure my script doesn't even get here
stoken = config(token=token);
req <- GET(sprintf("https://www.strava.com/api/v3/activities/%s",
clientId), stoken, client_id = clientId, client_secret = clientSecret, code = token);
stop_for_status(req); # not sure what this does
content(req); # not sure what this does
}发布于 2014-12-22 03:35:16
按顺序:
您没有在pullStravaData函数中使用任何类型的显式或隐式返回()。其结果是它实际上没有返回“数据”。试着转向:
data <- stravaPost(strava_token, clientId, clientSecret);分为:
data <- stravaPost(strava_token, clientId, clientSecret);
return(data)或者只是:
stravaPost(strava_token, clientId, clientSecret);..and看看会发生什么。顺便说一下,当我们在这里的时候,分号是不必要的。
我强烈建议,如果你要用R手动发出这类请求,你就会知道语言是如何运作的,它的规则是什么。这很不错,我听说。
发布于 2022-01-25 21:40:19
下面是从R访问strava的方式:
Client ID和Secret获得https://www.strava.com/settings/apilibrary(httr)
strava_endpoint <- oauth_endpoint(
request = NULL,
authorize = "authorize",
access = "token",
base_url = "https://www.strava.com/api/v3/oauth/"
)
myapp <- oauth_app(
"strava",
key = 11111, # <- change this to your strava ID
secret = "mysecretkey" # <- change this to your client ID
)
mytok <- oauth2.0_token(
endpoint = strava_endpoint,
app = myapp,
scope = c("activity:read"), # somehow it does not work with multiple scopes
cache = TRUE
)
resp <- GET("https://www.strava.com/api/v3/athlete", config(token = mytok))
jsonlite::fromJSON(rawToChar(resp$content))请随意将它包装成一个函数!
https://stackoverflow.com/questions/27596182
复制相似问题