LoginSignup
5
5

More than 5 years have passed since last update.

Electron で Pushbullet OAuth & API

Last updated at Posted at 2015-09-06

ElectronでPushBulletのOAuth認証を行います。

参考ページ

OAuth認証を行う

function getAccessToken() {
        const clientId = "YOUR_APPS_CLIENT_ID";

        // 認証ページを開くためのウィンドウ
        let BrowserWindow = require('remote').require('browser-window');
        let loginWindow = new BrowserWindow({
            width: 800,
            height: 600,
            'node-integration': false
        });

     // 認証後にリダイレクトするURL。ウィンドウは閉じるのでなんでもいい気がする
        // URLエンコードしないと無効なパラメータ扱いされた
        let redirect = "https%3A%2F%2Fwww.pushbullet.com%2Flogin-success";

     // 認証画面のURLを組み立てて、
        let url = `https://www.pushbullet.com/authorize?client_id=${clientId}&redirect_uri=${redirect}&response_type=token&scope=everything`;

     // 別ウィンドウで開く
        loginWindow.loadUrl(url);
        loginWindow.show();

     // 認証画面で認証が行われたら
        loginWindow.webContents.on('will-navigate', function(event, url) {
       // urlにaccesstokenが付随しているので取り出す
            var raw_code = /access_token=([^&]*)/.exec(url) || null,
                accessToken = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
                error = /\?error=(.+)$/.exec(url);
            if (accessToken || error) {
                loginWindow.close();
            }
            window.localStorage.setItem('pb_token', accessToken);
            return accessToken;
        });
    }

Pushbullet APIを使用する

Node.jsのPushbullet API moduleを使うと楽。
node-pushbullet-api

    let accessToken = getAccessToken();
    let PushBullet  = require('pushbullet');
    let pusher      = new PushBullet(accessToken);

    // Push履歴を取得する場合
    pusher.history({}, (err,res) => {
        this.pushHistory = res.pushes;
    })

思ったこと

  ClientSecret使わなかったけどええのん?

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5