Code Monkey home page Code Monkey logo

gatsby-source-strava-activities's Introduction

gatsby-source-strava-activities

NPM version Test build status

A Gatsby plugin that fetches athlete activities from Strava’s /athlete/activities endpoint. Learn more about the endpoint.

Usage

Install gatsby-source-strava-activities in your project:

yarn add gatsby-source-strava-activities
npm install gatsby-source-strava-activities

Then add the plugin to your gatsby-config.js file:

{
  resolve: "gatsby-source-strava-activities",
  options: {
    // `getRefreshToken` must look up and return the refresh token as a string.
    getRefreshToken: async () => {},
    // `onRefreshTokenChanged` runs each time the access token expired and
    // needed to be refreshed. It receives the new refresh token which must be
    // stored. It will be needed again next time the token expires.
    onRefreshTokenChanged: async (newRefreshToken) => {},
    // [Optional] An epoch timestamp to use for filtering activities that have
    // taken place after a certain time.
    after: '',
    // An epoch timestamp to use for filtering activities that have taken place
    // before a certain time.
    before: '',
  }
}

How to use getRefreshToken and onRefreshTokenChanged

Strava removed support for long-lived access tokens in October 2019. Instead of a long-lived access token, Strava provides short-lived access tokens and refresh tokens. The refresh tokens are exchanged for new access tokens when the access tokens expire. During this exchange, the response includes a refresh token that may or may not be different than the one just used. When the refresh token changes, the old one stops working. Because of this, you’ll need to store the refresh token somewhere that you can retrieve and update it at build time.

To get started, get the first refresh token with the activity:read scope by visiting https://www.strava.com/oauth/authorize?client_id=[client-id]&redirect_uri=[website]&response_type=code&scope=activity:read. You can use the activity:read_all scope if you want to include activities set to "Only Me" visibility. The [website] in that URL should be the site that is listed in your Strava application such as http://localhost:8000/. Once you click on "Authorize" you’ll get redirected to the [website] with a URL query parameter called code. The code is exchanged for a refresh token and short-lived access token with this command:

curl -X POST https://www.strava.com/api/v3/oauth/token \
  -d client_id=ReplaceWithClientID \
  -d client_secret=ReplaceWithClientSecret \
  -d code=ReplaceWithCode \
  -d grant_type=authorization_code

Strava will respond like this if it succeeds:

{
  "token_type": "Bearer",
  "expires_at": 1568775134,
  "expires_in": 21600,
  "refresh_token": "e5n567567...",
  "access_token": "a4b945687g...",
  "athlete": {
    #{summary athlete representation}
  }
}

The refresh_token in that response should be stored somewhere that can be accessed by getRefreshToken and onRefreshTokenChanged. getRefreshToken should fetch the newest refresh token and onRefreshTokenChanged should store the refresh token when it changes. Take a look at danoc/danoc.me for an example of implementing this with Firebase.

You can use allStravaActivity to query the data in GraphQL once the plugin is set up. Here’s an example of a query that fetches an activity’s name, Strava id, and distance.

{
  allStravaActivity {
    edges {
      node {
        activity {
          name
          id
          distance
        }
      }
    }
  }
}

The node contains the entire response from Strava’s endpoint.

gatsby-source-strava-activities's People

Contributors

danoc avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gatsby-source-strava-activities's Issues

Int cannot represent non-integer value

I'm pretty sure this is a gatsby error so also going to tag @KyleAMathews

{
      "message": "Int cannot represent non-integer value: 12.4",
      "locations": [
        {
          "line": 12,
          "column": 11
        }
      ],
      "path": [
        "allStravaActivity",
        "edges",
        1,
        "node",
        "activity",
        "max_speed"
      ]
    }
{
  allStravaActivity {
    edges {
      node {
        activity {
          id
          name
          start_date
          type
          distance
          average_speed
          max_speed
          elev_high
          elev_low
          kilojoules
          map { id }
        }
      }
    }
  }
}

.cache/redux-state.json:

63885     "Strava Activity: 1032425411": {
63886       "activity": {
63887         "resource_state": 2,
63888         "athlete": {
63889           "id": 5766809,
63890           "resource_state": 1
63891         },
63892         "name": "Afternoon Ride",
63893         "distance": 16968.6,
63894         "moving_time": 4128,
63895         "elapsed_time": 6080,
63896         "total_elevation_gain": 215.8,
63897         "type": "Ride",
63898         "workout_type": 10,
63899         "id": 1032425411,
63900         "external_id": "3768853e5fd55714ea948add4bae0876",
63901         "upload_id": 1135472738,
63902         "start_date": "2017-06-11T21:04:05Z",
63903         "start_date_local": "2017-06-11T14:04:05Z",
63904         "timezone": "(GMT-08:00) America/Vancouver",
63905         "utc_offset": -25200,
63906         "start_latlng": [
63907           49.23,
63908           -123.01
63909         ],
63910         "end_latlng": [
63911           49.28,
63912           -123.13
63913         ],
63914         "location_city": null,
63915         "location_state": null,
63916         "location_country": "Canada",
63917         "start_latitude": 49.23,
63918         "start_longitude": -123.01,
63919         "achievement_count": 3,
63920         "kudos_count": 1,
63921         "comment_count": 0,
63922         "athlete_count": 1,
63923         "photo_count": 0,
63924         "map": {
63925           "id": "a1032425411",
63926           "summary_polyline": "gxmkHfnxmVe@{@\\bCcCCt@n@m@aB_GrL}J~]aE`IpAfDCdRuMLkCnG_HvWaJvQQhEiCrC\\nC{IxT|@EiF|IcD|
      M\\|C_Ai@gCfFeDpSqBjCIvLcDHy[jaAiKuFcCdG_C_CqAvBkRt@eCkBkBzEt@vDmP@_GhTwAl@DlD}CxF}Rhz@fGCGdKuAz@YhUuCh]sCbIt@lb@s@gBuB
      Tk@fJwM~Vi@bLvA@w@xJuH_@ScG_HoC_Iz@yC`FK~Hr@b@wA`RbErCfJvXUpF~AfCgEtRpD~FdFz@nCtFuEzKEnGhG`KCfDiEnG?`DaB|CaH`G}DxHsFeKs
      DWbIbDwBoDoKlBjA?",
63927           "resource_state": 2
63928         },
63929         "trainer": false,
63930         "commute": false,
63931         "manual": false,
63932         "private": false,
63933         "flagged": false,
63934         "gear_id": null,
63935         "from_accepted_tag": false,
63936         "average_speed": 4.111,
63937         "max_speed": 12.4,
63938         "average_watts": 125.2,
63939         "kilojoules": 516.9,
63940         "device_watts": false,
63941         "has_heartrate": false,
63942         "elev_high": 155.7,
63943         "elev_low": -2,
63944         "pr_count": 0,
63945         "total_photo_count": 0,
63946         "has_kudoed": false
63947       },

regeneratorRuntime is not defined with gatsby v2

Hi, When I try to use your plugin, I get this error:

error Plugin gatsby-source-strava-activities returned an error
  ReferenceError: regeneratorRuntime is not defined

I tried to include import '@babel/polyfill' at the top of my gatsby-browser.js and to modify my gatsby-node.js with:

exports.onCreateBabelConfig = ({ babelrc }) => ({
  ...babelrc,
  plugins: babelrc.plugins.concat(['@babel/plugin-transform-regenerator']),
})

But it does not work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.