Grabbing strava data using R, having trouble with authentication, R version 3.1.2

772 views
Skip to first unread message

Jerry Tyrrell

unread,
Dec 23, 2014, 10:33:00 PM12/23/14
to strav...@googlegroups.com
Not sure what is going wrong here, been playing with it for a couple of days now. This is the closest I've gotten to a working bit of code.

clientSecret <- "blahblahblah"
clientId <- "3979" # thats me
myapp <- oauth_app("strava", clientId, clientSecret)
authorize <- "http://localhost:1410"
endpoint <- oauth_endpoint(request, authorize, access)
z <- (oauth2.0_token(endpoint, myapp, scope="view_private"))

The result is "z"

> z
<Token>
<oauth_endpoint>
<oauth_app> strava
  key:    3979
  secret: <hidden>
<credentials> 
---

> z$credentials # returns about 15,000 lines of this, which when parsed to characters is just the html of the main strava login page...
    [1] 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 63 6c 61 73 73 3d
   [29] 27 6c 6f 67 67 65 64 2d 6f 75 74 20 66 75 6c 6c 73 63 72 65 65 6e 20 72 65 73 70 6f
   [57] 6e 73 69 

> c <-rawToChar(z$credentials, multiple = FALSE)
> c
[1] "<!DOCTYPE html>\n<html class='logged-out fullscreen responsive cycling-background-1 old-login' lang='en-US' xmlns:fb='http://www.facebook.com/2008/fbml' xmlns:og='http://opengraphprotocol.org/schema/' xmlns='http://www.w3.org/TR/html5'>\n<head>\n<meta charset='UTF-8'>\n<meta content='i-49ea2da4' name='instance_id'>\n<meta content='yes' name='apple-mobile-web-app-capable'>\n<meta content='black' name='apple-mobile-web-app-status-bar-style'>\n<meta content='width = device-width, initial........


Has anyone connected to strava with R before? Got any advice? Thanks, I appreciate it!


Michael Henasey

unread,
Dec 24, 2014, 7:37:42 AM12/24/14
to strav...@googlegroups.com
What is 'R'?

When you want to authorize with Strava, you need to first login as a Strava user who will be authorizing your app to access this user's data. The page you are getting back is the Strava login page. After your user logs into Strava, you will be sent to the authorization page for your app where the user can see what type of access your app is asking for. Here is where the user will either authorize or deny access to your app. Once the user has authorized the app, you will receive an authorization code which you will use to ask for an authorization token, which you can save/cache for all future requests to the API.

Paul Mach

unread,
Dec 24, 2014, 1:19:00 PM12/24/14
to Michael Henasey, strava-api
Hi Jerry,

Like Michael said, you're getting the login page. This "flow" is to have other users authorize with your app. I'm guessing, since you're using R, you want to just look at your own data. In that case you can go to http://www.strava.com/settings/api to find your access token. Then make requests to the api directly using that token.  That way you won't have to mess with all the oauth stuff.

Dr. Paul Mach
STRAVA

--
You received this message because you are subscribed to the Google Groups "Strava API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strava-api+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carson Forter

unread,
Jan 2, 2015, 12:26:25 PM1/2/15
to strav...@googlegroups.com
Hi Jerry,

Just wanted to chime in because I've been accessing the Strava API from R for the last week for a school project. Like Paul suggested in his post, I'm just using this for public segment/leaderboard data, not authorizing for users other than myself. I'm using the httr package. The code below should get you started:

> install.packages("httr")
> library(httr)
> data <- GET("https://www.strava.com/api/v3/segments/668184/leaderboard", add_headers(Authorization = "Bearer *INSERT YOUR ACCESS TOKEN* "), query = list(page = 1, per_page = 100))
> leaderboard <- content(data, as = "parsed")

This gets the first 100 entries in the leaderboard specified by the url. You can change the the number in the url to get whichever segment's data you're interested in. Happy to answer any other questions if I'm able.

Carson

Mathieu Boidot

unread,
Nov 17, 2017, 2:59:03 PM11/17/17
to Strava API
Hi Paul,

This is a rather old topic, but i'm just starting to try to use the strava api with R.

Just like Jerry I've set up my R code as this :

my_app <- oauth_app("my_app_name",key = "my_key",secret = "my_secret")
my_endpoint
<- oauth_endpoint(request = NULL,authorize = "https://www.strava.com/oauth/authorize",access = "https://www.strava.com/oauth/token")
sig
<- oauth2.0_token(my_endpoint, my_app, scope = "view_private",  type = NULL, use_oob = FALSE, as_header = FALSE,   use_basic_auth = FALSE, cache = FALSE)


When I execute this code, the authorization page loads in my browser, then I click on the "authorize" button, and my browser tells me to return to R. And in R i get this error message :

Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope,  : (HTTP 401). Failed to get an access token.


So, Paul, as I already have my access token because I registered an app, can you tell me how to make requests using this token ?

Thanks

Mathieu
Reply all
Reply to author
Forward
0 new messages