Giter Site home page Giter Site logo

slack-oauth2's Introduction

slack-oauth2

A simple library to quickly acquire access tokens following oauth2 protocol.

Please refer https://api.slack.com/docs/oauth for the details on working of Oauth2.0 on slack

Tested with slack oauth2 authentication.

Installation

You can install using npm

npm install https://github.com/koustubh25/slack-oauth2.git --save

Sample code

In express

var express = require('express');
var Client = require('oauth2');
var client = new Client();
client.createCredentials({
    "client_id" : process.env.CLIENT_ID,
    "client_secret" : process.env.CLIENT_SECRET,
    "scope": ["channels:read", "chat:write:bot"],
    "redirect_url": "<your redirect url>"
});
client.setAuthEndPoint("https://slack.com/oauth/authorize");
client.setOauthEndPoint("https://slack.com/api/oauth.access");

var app = express();

app.get('/<add slack button page>', (req, res, next) => {
    res.write('<a href="' + client.getAuthUrl() + '">
                <img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x" />
                </a>');
    res.end();
});

app.get('/<your redirect url>', (req, res, next) => {
    let code = req.param("code");
    let state = req.param("state");
    let access_token = client.getAccessToken(code, state).then((result) => {
       console.log(result);
    })
        .catch((err) => {
            console.log(err);
        });
    res.write('<h1> My Page </h1>);
    res.end();

});

Result contains code like the following:

{
    "ok":true,
    "access_token":"<Your access token>",
    "scope":"channels:read,chat:write:bot",
    "user_id":"<some user id>",
    "team_name":"<some name>",
    "team_id":"<some id>"
}

slack-oauth2's People

Contributors

koustubh25 avatar

Watchers

 avatar  avatar

Forkers

vegabid

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.