Created
February 23, 2019 10:01
-
-
Save r3-yamauchi/45adf0c04929db319eb4deebcf52cb64 to your computer and use it in GitHub Desktop.
LIFF を使って LINE から簡単に kintone データにアクセスする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const createResponse = (statusCode, content) => { | |
const body = JSON.stringify(content); | |
const response = { | |
"statusCode": statusCode, | |
"headers": { | |
"Access-Control-Allow-Origin": "*" | |
}, | |
"body": body | |
}; | |
console.log(JSON.stringify(response, null, 2)); | |
return response; | |
}; | |
exports.handler = async (event) => { | |
console.log(JSON.stringify(event, null, 2)); | |
if (!event.headers.accesstoken) { | |
return createResponse(401, { "message": "Unauthorized" }); | |
} | |
const accessToken = event.headers.accesstoken; | |
const url = `https://api.line.me/oauth2/v2.1/verify?access_token=${accessToken}`; | |
console.log(`url: ${url}`); | |
return axios.get(url) | |
.then(res => { | |
console.log(res.data); | |
if (!res.data.scope) { | |
return Promise.reject(new Error("Not Authorized")); | |
} | |
return axios({ | |
method: "GET", | |
url: 'https://api.line.me/v2/profile', | |
headers: { | |
'Authorization': `Bearer ${accessToken}` | |
} | |
}); | |
}).then(res => { | |
console.log(res.data); | |
console.log(`userId: ${res.data.userId}`); | |
console.log(`displayName: ${res.data.displayName}`); | |
return createResponse(200, event); | |
}).catch(err => { | |
return createResponse(500, err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment