78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
const { getAccessToken } = require('./modules/lineworksAuth');
|
|
|
|
const USER_INFO_SCOPES = ['user.read', 'directory.read'];
|
|
|
|
async function main() {
|
|
const [, , identifier, domainArg] = process.argv;
|
|
if (!identifier) {
|
|
console.error('使い方: node lookupLineworksUser.js <userId|email|externalKey:xxx> [domainId]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const domainId = domainArg || process.env.LINEWORKS_DOMAIN_ID || '';
|
|
const encodedIdentifier = encodeURIComponent(identifier);
|
|
|
|
const accessToken = await getAccessToken(USER_INFO_SCOPES);
|
|
const baseUrl = `https://www.worksapis.com/v1.0/users/${encodedIdentifier}`;
|
|
const apiUrl = domainId ? `${baseUrl}?domainId=${encodeURIComponent(domainId)}` : baseUrl;
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`
|
|
}
|
|
});
|
|
|
|
const text = await response.text();
|
|
let data;
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
console.error('LINE WORKS APIレスポンス解析に失敗しました:', text);
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!response.ok) {
|
|
console.error('LINE WORKS APIエラー:', JSON.stringify(data, null, 2));
|
|
process.exit(1);
|
|
}
|
|
|
|
printUserInfo(data);
|
|
}
|
|
|
|
function printUserInfo(user) {
|
|
console.log('--- User Info ---');
|
|
console.log(`domainId : ${user.domainId}`);
|
|
console.log(`userId : ${user.userId}`);
|
|
console.log(`userExternalKey : ${user.userExternalKey ?? 'N/A'}`);
|
|
console.log(`email : ${user.email}`);
|
|
console.log(`name : ${formatName(user.userName)}`);
|
|
console.log(`nickName : ${user.nickName ?? 'N/A'}`);
|
|
console.log(`employeeNumber : ${user.employeeNumber ?? 'N/A'}`);
|
|
console.log(`isAdministrator : ${user.isAdministrator}`);
|
|
console.log(`isSuspended : ${user.isSuspended}`);
|
|
console.log(`isDeleted : ${user.isDeleted}`);
|
|
|
|
if (Array.isArray(user.organizations)) {
|
|
console.log('organizations:');
|
|
for (const org of user.organizations) {
|
|
console.log(` - domainId : ${org.domainId}, primary: ${org.primary}, email: ${org.email ?? 'N/A'}`);
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(user.aliasEmails) && user.aliasEmails.length > 0) {
|
|
console.log(`aliasEmails : ${user.aliasEmails.join(', ')}`);
|
|
}
|
|
}
|
|
|
|
function formatName(userName = {}) {
|
|
const last = userName.lastName ?? '';
|
|
const first = userName.firstName ?? '';
|
|
return `${last} ${first}`.trim() || 'N/A';
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error('ユーザー照会でエラーが発生しました:', error.message);
|
|
process.exit(1);
|
|
});
|