LoginSignup
69
67

More than 5 years have passed since last update.

Google Drive Web APIを使う(node編)

Posted at

アナログなKanbanの状態を定時に写真を撮って保存して、上海のチームメンバーと共有するため、Raspberry Piにhubotをいれてアップするこのシリーズ。今回はGoogle Drive Web APIを使って撮った写真を保存するやり方をまとめておく。

参考リンク

認証について

  • Google APIの認証には大きく分けて2つある。OAuthとSimple API Key
  • Google Drive内のリソースにアクセスするので、Simple Keyは使えない

OAuth2認証

アクセストークンの取得

  • APIのアクセスに必要なaccess_tokenを取得するため、まず下記のコードを書く。
get_tokens.js
var google = require('googleapis'),
    readline = require('readline');

var CLIENT_ID = 'あなたのアプリケーションのCLIENT_ID',
    CLIENT_SECRET = 'あなたのアプリケーションのCLIENT_SECRET',
    REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob',
    SCOPE = [
      'あなたのアプリケーションで必要なスコープを配列形式で',
      'https://www.googleapis.com/auth/drive.file'
    ];

var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function getAccessToken(oauth2Client) {
  // OAuth2認証のためのURLを生成する
  var url = oauth2Client.generateAuthUrl({
    access_type: 'offline', // refresh_tokenが必要なので、offlineを指定
    scope: SCOPE 
  });

  console.log('右記のURLをブラウザで開いてください: ', url);
  rl.question('表示されたコードを貼り付けてください:', function(code) {
    // request access token
    oauth2Client.getToken(code, function(err, tokens) {
      console.log('トークンが発行されました');
      console.log(tokens);
      console.log('上記の情報を大切に保管してください');
    });
  });
}

var fs = require('fs');

getAccessToken(oauth2Client);

  • get_tokens.jsを実行する。
$ node get_tokens.js
  • 指示されたとおり、URLを開くと、OAuth認証のページが開く

許可のリクエスト.png

  • 承認するとこんな感じでアクセストークンを取得するためのトークンが発行される。

Success_code.png

  • このトークンを先ほどのスクリプトの「表示されたコードを貼り付けてください:」の右に貼り付ける
  • するとaccess_tokenとrefresh_tokenが発行される。access_tokenの有効期間は1時間。
    • 今回は多くても1時間に1回アクセスしないので、必ずrefresh_tokenを使ってaccess_tokenを取得するようにした。

スコープの話

Google Drive Web APIにあるスコープは下記の通り

Scope 意味
https://www.googleapis.com/auth/drive.file アプリケーションからファイルを作成する、もしくは開くことができる(Per-file access to files created or opened by the app)
https://www.googleapis.com/auth/drive 完全にユーザーのファイルにアクセスすることがゆるされるスコープ。どうしても必要な時にだけこのスコープを要求する。https://docs.google.com/feedsのスコープに許可されたトークンは、https://www.googleapis.com/auth/driveのスコープのトークンとと同じように扱う。(Full, permissive scope to access all of a user's files. Request this scope only when it is strictly necessary. Tokens with scope https://docs.google.com/feeds are accepted and treated the same as tokens with scope https://www.googleapis.com/auth/drive.)
https://www.googleapis.com/auth/drive.apps.readonly ユーザーがインストールしたアプリケーションの一覧に対し読み込み専用でアクセスを許可する (Allows apps read-only access to the list of Drive apps a user has installed.)
https://www.googleapis.com/auth/drive.readonly ファイル自体とそのメタ情報に読み込み専用でアクセスを許可する (Allows read-only access to file metadata and file content)
https://www.googleapis.com/auth/drive.readonly.metadata ファイルのメタ情報の読み込みは許可するが、ファイルのダウンロードや読み込みは許可しない (Allows read-only access to file metadata, but does not allow any access to read or download file content)
https://www.googleapis.com/auth/drive.install ユーザーにアプリケーションのインストールを進める特別なスコープ (Special scope used to let users approve installation of an app)
https://www.googleapis.com/auth/drive.appfolder アプリケーションのデータフォルダにアクセスすることを許可するスコープ (Allows access to the Application Data folder)
https://www.googleapis.com/auth/drive.scripts Google Appsのスクリプトファイルへのアクセスを許可(Allows access to Apps Script files)

APIのメモ

  • Google API ExplorerでAPIのお試しができる
  • File#insert (Doc) でアップロード
    • 特定のパスにアップロードしたい場合は下記のようにparentsを指定する
  • File#list (Doc) でDriveのリソースを一覧で取得
    • qというパラメータが検索条件を指定するクエリ。パラメータについてはこちら
    • 例えばフォルダだけ取得したい場合はmimeType = 'application/vnd.google-apps.folder'と指定する

コード例

  • CLIENT_ID,CLIENT_SECRET,REDIRECT_URL,REFRESH_TOKENは置き換える
upload_file.js
var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
oauth2Client.setCredentials({
  refresh_token: REFRESH_TOKEN
});

oauth2Client.refreshAccessToken(function(err,res){
  var drive = google.drive({ version: 'v2', auth: oauth2Client });
  drive.files.list({
    q: "title='kanban'"
  },function(err,res){

    // kanbanという名前のフォルダを探してIDを取得する
    var folderId = res.items[0].id

    var fs = require('fs');
    var file = fs.readFileSync('画像.jpg');

    // アップロードの日時からファイル名を作成
    var filename = new Date().toISOString() + ".jpg" 

    // ファイルのアップロード resourceでパラメータを指定するのがなかなかわからなかった
    drive.files.insert({
      auth: oauth2Client,
      resource: {
        title: filename,
        mimeType: 'image/jpeg',
        parents: [
          {
            id: folderId
          }
        ]
      },
      media: {
        mimeType: 'image/jpeg',
        body: file
      }
    }, function(err,res){
      if(err !== undefined && err !== null){
        console.error(err);
      }
      console.log(res)
      // 下記のURLでアップロードしたファイルに直接アクセスできるのはファイルのパーミッションがpublicの時のみ
      console.log('https://docs.google.com/a/ドメイン名/uc?id='+res.id);
    });
  });
});
69
67
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
69
67