Code Monkey home page Code Monkey logo

backlog-gs's Introduction

Backlog-GS clasp

Backlog API version 2 client for Apps Script

Required reading

Please check out the Nulab Developers portal page.

Installation

Clone this repository

Getting started

Append your "API Key" or "OAuth2 Access Token" to requests to the API to return data. The example is in Typescript.

// 'xxx.backlog.jp' or 'xxx.backlogtool.com' or 'your package host'
const host = 'yourSpaceHost';
const apiKey = 'yourApiKey';
const accessToken = 'yourAccessToken';

// Use API Key
const backlog = new Backlog({ host, apikey });

// Use OAuth2 Access Token
const backlog = new Backlog({ host, accessToken });

// Returns information about your space.
backlog.getSpace().then(data => {
  Logger.log('space: %s', data);
}).catch(err => {
  Logger.log('error: %s', err.message);
});

Use OAuth2 for authentication

Details on the OAuth2 process are available here. See App.ts for complete source code

Here are the basic steps for OAuth2 using the Apps Script:

function doGet(request: RequestEvent): GoogleAppsScript.Content.TextOutput | GoogleAppsScript.HTML.HtmlOutput {
    let params = request.parameter;
    let action = params.action ? params.action.toLowerCase() : '';
    let response: GoogleAppsScript.Content.TextOutput | GoogleAppsScript.HTML.HtmlOutput;
    let propSvc = PropertiesService.getScriptProperties();
    let clientId = propSvc.getProperty('backlog.clientId');
    let clientSecret = propSvc.getProperty('backlog.clientSecret');
    let host = propSvc.getProperty('backlog.host');
    let redirectUri = `${ScriptApp.getService().getUrl()}?action=callback`
    let oauth2 = new OAuth2({ clientId, clientSecret });

    switch (action) {
        case 'login':
            // KNOWN ISSUE: Backlog does not support redirect_url parameter 
            response = redirect(oauth2.getAuthorizationURL({ host }));
            break;
        case 'callback':
            let code: string = params.code;
            response = onCallback(oauth2, host, code, redirectUri);
            break;
        default:
            response = render('index', null, APP_NAME);
            break;
    }

    return response;
}

function onCallback(oauth2: OAuth2, host: string, code: string, redirectUri?: string): GoogleAppsScript.Content.TextOutput | GoogleAppsScript.HTML.HtmlOutput {
    try {
        let accessToken: Entity.OAuth2.AccessToken;
        oauth2.getAccessToken({ host, code }).then(value => accessToken = value);
        Logger.log('Access Token: %s', accessToken);

        // Save access token.
        let myself: any;
        let bl = new Backlog({ host, accessToken: accessToken.access_token });
        bl.getMyself().then(value => myself = value);
        Logger.log('Myself: %s', myself);

        let propSvc = PropertiesService.getScriptProperties();
        propSvc.setProperty('backlog.accessToken', accessToken.access_token);
        return render('success', null, APP_NAME);
    } catch (e) {
        Logger.log('Access Token Error: %s', e.message);
        return render('error', null, APP_NAME);
    }
}

TODO

Download File

Upload File

Inspired by

License

MIT License

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.