Skip to content

Instantly share code, notes, and snippets.

@chj1768
Created June 12, 2017 02:15
Show Gist options
  • Save chj1768/05c493a15874ccd11620115129138c52 to your computer and use it in GitHub Desktop.
Save chj1768/05c493a15874ccd11620115129138c52 to your computer and use it in GitHub Desktop.
Meteor.js에서 구글 서비스 계정 인증 + 구글시트 연동 관련 로직
import { HTTP } from 'meteor/http'
let google = require('googleapis');
const serviceId = 'xxx-service-account@true-tooling-108006.iam.gserviceaccount.com';
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