Created
June 12, 2017 02:15
-
-
Save chj1768/05c493a15874ccd11620115129138c52 to your computer and use it in GitHub Desktop.
Meteor.js에서 구글 서비스 계정 인증 + 구글시트 연동 관련 로직
This file contains 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
import { HTTP } from 'meteor/http' | |
let google = require('googleapis'); | |
const serviceId = '[email protected]'; | |
const keyFileName = 'xxx_service_account.json'; | |
const scopeUrl = 'https://www.googleapis.com/auth/spreadsheets'; | |
const jwtClient = new google.auth.JWT( | |
serviceId, Assets.absoluteFilePath( keyFileName ), null, [ scopeUrl ] | |
); | |
jwtClient.authorize( Meteor.bindEnvironment( ( err, tokens ) => { | |
if ( err ) { | |
console.log( err ); | |
return; | |
} | |
const sheetsUrl = 'https://sheets.googleapis.com/v4/spreadsheets'; | |
const sheetsId = 'sheetsId'; | |
HTTP.get( sheetsUrl + '/' + sheetsId + '/values/A:B', | |
{ | |
params : { 'majorDimension' : 'ROWS' }, | |
headers : { 'Content-Type': 'application/json' ,'Authorization': 'Bearer ' + tokens.access_token } | |
}, | |
( er, re ) => { | |
if ( er || !re.data.values ) { | |
console.log(er); | |
return; | |
} | |
const lastDataIndex = re.data.values.pop(); //순서 번호 매기기 관련 | |
const data = [ //행별로 정해진 데이터 넣어주기 (예시) | |
parseInt( lastDataIndex[ 1 ] ) + 1, | |
requestDate, | |
userName, | |
userEmail, | |
local, | |
aptName, | |
complex, | |
area, | |
type, | |
'', '', '', '', '', '', '', | |
category | |
]; | |
HTTP.post( sheetsUrl + '/' + sheetsId + '/values/A:V:append', | |
{ | |
params : { 'valueInputOption' : 'USER_ENTERED', 'insertDataOption' : 'OVERWRITE' }, | |
headers : { | |
'Content-Type' : 'application/json', | |
'Authorization' : 'Bearer ' + tokens.access_token | |
}, | |
data : { 'values' : [ data ] } | |
}, | |
( er, re ) => { | |
if ( er ) { console.log( er ); } | |
} | |
); | |
} | |
); | |
} ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment