LoginSignup
4
0

More than 1 year has passed since last update.

Node.js(axios)からDirectCloud-BOX APIでファイルのDL #linedc

Last updated at Posted at 2021-08-26

DirectCloud-BOX APIのAPIを触ってみてました。

前回の記事までで、トークン取得、ファイルのアップロードができてるのでファイル取得も試してみます。

ファイルのダウンロードのAPIを使ってみました。

downloadのAPIをNode.jsから

基本はアップロードのときと同様ですが、file_seqの値を入れてあげる必要があります。

アップロードしたときのレスポンスに値があるのでそれを使えば大丈夫です。

'use strcit';

const axios = require('axios');
const FormData = require('form-data');

const token = `xxxxxxxxx`;

const BASE_URL = `https://api.directcloud.jp`;
const URL = `${BASE_URL}/openapp/v1/files/download/1?lang=eng`

const main = async () => {

    const bodyFormData = new FormData();

    const file_seq = 000000000; //アップロードしたときにresponseで帰ってくる値です。
    bodyFormData.append('file_seq', file_seq);

    const res = await axios.post(URL, bodyFormData, {
        headers: {
            access_token: token,
            ...bodyFormData.getHeaders()
        }
    });
    console.log(res.data);
};

main();

実行してみると、URLが発行されました。

$ node playground/dl.js 
{
  success: true,
  download_url: 'https://cdn.directcloud.jp/mdmvslth/tvwvmsgt/20210825202948435483?response-con...'
}

ブラウザでアクセスすると直接DLしようとしてきます。

スクリーンショット 2021-08-26 12.20.11.png

おまけ LINEで表示する

このURLをLINE Messaging APIの画像メッセージに設定すればLINE上で画像表示もできます。

const url = `API経由で取得した、DirectCloud-BOXのDLリンク`
const messages = [{
    type: 'image',
    originalContentUrl: url,
    previewImageUrl: url
}];

参考: Node.jsでLINE BOTの画像送信を試す

動かすとこんな感じ。2件目が画像です。1件目はテストでテキストを送ってます。

スクリーンショット 2021-08-26 12.35.24.png

コピペ用

ほぼこちらのコードくらいなので書くほどでもないでが一応コピペ用を置いておきます。

友達全員にメッセージを送るシンプルな送信専用LINE BOTを作る【Node.js】 #linedc

'use strict';

const line = require('@line/bot-sdk');

const config = {
    channelSecret: 'LINE Botのチャンネルシークレット',
    channelAccessToken: 'LINE Botのチャンネルアクセストークン'
};
const client = new line.Client(config);


const main = async () => {
    const url = `https://cdn.directcloud.jp/mdm~~~~~`
    const messages = [{
        type: 'image',
        originalContentUrl: url,
        previewImageUrl: url
    }];

    try {
        const res = await client.broadcast(messages);
        console.log(res);        
    } catch (error) {
        console.log(`エラー: ${error.statusMessage}`);
        console.log(error.originalError.response.data);
    }
}

main();

所感など

LINEで見れるのはやはり便利!

↓こんなの作ってます。

紙で申請を出す居酒屋店員のシフト希望と管理業務をちょっとスマートにする
https://protopedia.net/prototype/2384

4
0
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
4
0