💬
[GAS]jinjerからAPIで取得
はじめに
GoogleAppsScript(GAS)を利用してjinjerAPIにアクセスし、従業員情報を自動で取得する。
なお、jinjerはトークン(有効期間は4時間)が必要になるので、
- tokenを取得
- tokenを元に従業員情報を取得
を行った。
準備
jinjerの管理画面等からAPI利用に関する設定を行い、「APIキー」と「シークレットキー」を取得。
code
main.js
function main(){
const jinjerManager = new JinjerManager();
// 1. tokenを取得
jinjerManager.getToken();
// 2. tokenを元に従業員情報を取得
jinjerManager.getEmployees();
}
class/JinjerManager.js
class JinjerManager{
constructor() {
this.apiKey = PropertiesService.getScriptProperties().getProperty('apiKey');
this.apiSecretKey = PropertiesService.getScriptProperties().getProperty('apiSecretKey');
this.token;
}
getToken() {
// https://doc.api.jinjer.biz/index.html#tag/%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%83%88%E3%83%BC%E3%82%AF%E3%83%B3
const options = {
headers : {
'Content-Type': 'application/json',
'X-API-KEY': this.apiKey,
'X-SECRET-KEY': this.apiSecretKey,
},
method : 'get',
};
this.token = JSON.parse(UrlFetchApp.fetch(`https://api.jinjer.biz/v2/token`, options)).data.access_token;
}
getEmployees(){
// https://doc.api.jinjer.biz/index.html#tag/%E5%BE%93%E6%A5%AD%E5%93%A1/operation/employeesGet
const options = {
headers : {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`,
},
method : 'get',
};
const res = JSON.parse(UrlFetchApp.fetch(`https://api.jinjer.biz/v1/employees`, options));
Logger.log(res);
}
}
参考資料
終わりに
従業員情報以外にも評価など、色々な情報が取得できるのでとてもありがたい。
JinjerManagerはOSSにしたいな。
Discussion