LoginSignup
14
15

More than 5 years have passed since last update.

Node.js で Github のOAuth アクセストークンを取得する

Last updated at Posted at 2012-11-21

殴り書きで失礼。。。

コンソールから GithubAPI を叩くための第一歩。
Basic認証でアクセストークンを取得する。

この方法だとアプリ登録の必要がないのと、コマンドだけで済むので楽。

github.js
#!/usr/local/bin/node
var https        = require('https');

var user     = 'USER_NAME',
    password = 'PASSWORD';

var options = {
    host: 'api.github.com',
    port: 443,
    method: 'POST',
    path: '/authorizations',
    auth: user + ':' + password
};

var params = {
    scopes: ['public_repo', 'user', 'gist']
};

var request = https.request(options);

request.on('response', function (response) {
    response.setEncoding('utf8');

    var body = "";
    response.on('data', function (chunk) {
        body += chunk;
    });
    response.on('end', function () {
        console.log(JSON.parse(body));
    });
});

request.on('error', function (error) {
    console.log('problem with request: ', error);
    process.exit();
});

var body = JSON.stringify(params);

request.setHeader('Content-Length', Buffer.byteLength(body));
request.write(body + '\n');
request.end();

実行するとマニュアルの通りのレスポンスが取得できる。

参考)

@todo

  • コードのリファクタリング、構造化
  • トークンを使ってAPIをいろいろ叩く
  • ユーザーID, パスワードをパラメータで受け取れるようにする
14
15
2

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
14
15