Git Product home page Git Product logo

strava-oauth2's Introduction

strava-oauth2

A simple Node.JS OAuth2 client for connecting your app to the Strava API

Installation

Using npm:

npm install strava-oauth2

API Examples

Class Instantiation

strava-oauth2 exposes 2 classes which can be used to interact with the Strava authentication services.

A full list of configuration options and defaults are provided in Config Defaults.

const { Client, Token } = require('strava-oauth2');

// The below configuration is the minimum required.
const config = {
    client_id: 194012,
    client_secret: 'abcdef1234567890',
    redirect_uri: 'https://localhost/auth/callback'
};

const client = new Client(config);

Client Authentication

By URL

The below uses Express to redirect a connecting client to the Strava authentication page before parsing the resultant code and obtaining a Token.

app.get('/auth', (req, res) => {
    res.redirect(client.getAuthorizationUri());
});

// Must be the same as the redirect_uri specified in the config
app.get('/auth/callback', async (req, res) => {
    const token = await client.getToken(req.originalUrl);
    // Process token...

    res.redirect('/home');
});

app.get('/home', (req, res) => {
    res.send('Welcome!');
});

By Request Parameters

You can also parse the request query string parameters yourself and pass them to strava-oauth2, via Client.getTokenFromObject(params). The example below generates a token via the event passed by an AWS API Gateway Lambda Proxy integration.

exports.handler = async (event) => {
    const params = event.queryStringParameters;
    console.info('Here are our params', params);
    const token = await client.getTokenFromObject(params);
}

Console output:

Here are our params {
  state: '',
  code: 'abcdef1234567890',
  scope: 'read,activity:read_all'
}

Request Signing

To interact with the Strava API, you must sign your request with the access token. The Token class can provide an axios client which signs all requests made with it using your access token.

if (!token.hasExpired()) {
    // If the token has expired the below will throw an exception
    const axios_instance = token.getSignedAxiosInstance();

    const athlete = axios_instance.get('https://www.strava.com/api/v3/athlete');
}

Config Defaults

Full list of default configuration options and defaults is below. If you instantiate a Client class without specifying a client_id and a client_secret, an exception will be thrown. The other parameters are optional.

const config = {
    authorization_uri: 'https://www.strava.com/api/v3/oauth/authorize',
    token_uri: 'https://www.strava.com/api/v3/oauth/token',
    revocation_uri: 'https://www.strava.com/api/v3/oauth/deauthorize',
    client_id: null,
    client_secret: null,
    redirect_uri: 'https://localhost',
    scopes: ['read'],
}

strava-oauth2's People

Contributors

tobydickinson avatar

Watchers

 avatar  avatar

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.