2016-12-28 5 views
2

작은 프로젝트를 개발 중이며 사용 편의성을 위해 Google 드라이브를 사용하고 싶습니다. 프로그램의 주된 전제는 람다 함수가 활성화되었을 때 Google 시트에 새로운 행을 삽입하는 것입니다. 나는이 프로젝트에 Node.js를 사용하는 것을 선호하지만 Java 또는 Python을 열어 둔다.AWS 람다에서 Google 시트 수정

tutorial 사이트에서이 모든 것이 어떻게 작동하는지 쉽게 알 수 있습니다. 당신은 요청을하고 OAuth를 가졌으며 프로그램은 그 내용을 그대로 유지합니다. 그러나, 내 Google 드라이브에있는 폴더와 AWS 람다 함수 대화를하고 시트를 자유롭게 업데이트 할 수있는 방법을 찾고 있습니다.

다음과 같이 튜토리얼의 코드는 다음과 같습니다 내가를 선택하지 않고 내 구글 드라이브 폴더에 특별한 권한이 액세스를 람다 기능을 제공 할 수있는 몇 가지 방법이있을거야하고있다

var fs = require('fs'); 
var readline = require('readline'); 
var google = require('googleapis'); 
var googleAuth = require('google-auth-library'); 

// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/drive-nodejs-quickstart.json 
var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']; 
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || 
    process.env.USERPROFILE) + '/.credentials/'; 
var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.json'; 

// Load client secrets from a local file. 
fs.readFile('client_secret.json', function processClientSecrets(err, content) { 
    if (err) { 
    console.log('Error loading client secret file: ' + err); 
    return; 
    } 
    // Authorize a client with the loaded credentials, then call the 
    // Drive API. 
    authorize(JSON.parse(content), listFiles); 
}); 

/** 
* Create an OAuth2 client with the given credentials, and then execute the 
* given callback function. 
* 
* @param {Object} credentials The authorization client credentials. 
* @param {function} callback The callback to call with the authorized client. 
*/ 
function authorize(credentials, callback) { 
    var clientSecret = credentials.installed.client_secret; 
    var clientId = credentials.installed.client_id; 
    var redirectUrl = credentials.installed.redirect_uris[0]; 
    var auth = new googleAuth(); 
    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 

    // Check if we have previously stored a token. 
    fs.readFile(TOKEN_PATH, function(err, token) { 
    if (err) { 
     getNewToken(oauth2Client, callback); 
    } else { 
     oauth2Client.credentials = JSON.parse(token); 
     callback(oauth2Client); 
    } 
    }); 
} 

/** 
* Get and store new token after prompting for user authorization, and then 
* execute the given callback with the authorized OAuth2 client. 
* 
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. 
* @param {getEventsCallback} callback The callback to call with the authorized 
*  client. 
*/ 
function getNewToken(oauth2Client, callback) { 
    var authUrl = oauth2Client.generateAuthUrl({ 
    access_type: 'offline', 
    scope: SCOPES 
    }); 
    console.log('Authorize this app by visiting this url: ', authUrl); 
    var rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
    }); 
    rl.question('Enter the code from that page here: ', function(code) { 
    rl.close(); 
    oauth2Client.getToken(code, function(err, token) { 
     if (err) { 
     console.log('Error while trying to retrieve access token', err); 
     return; 
     } 
     oauth2Client.credentials = token; 
     storeToken(token); 
     callback(oauth2Client); 
    }); 
    }); 
} 

/** 
* Store token to disk be used in later program executions. 
* 
* @param {Object} token The token to store to disk. 
*/ 
function storeToken(token) { 
    try { 
    fs.mkdirSync(TOKEN_DIR); 
    } catch (err) { 
    if (err.code != 'EEXIST') { 
     throw err; 
    } 
    } 
    fs.writeFile(TOKEN_PATH, JSON.stringify(token)); 
    console.log('Token stored to ' + TOKEN_PATH); 
} 

/** 
* Lists the names and IDs of up to 10 files. 
* 
* @param {google.auth.OAuth2} auth An authorized OAuth2 client. 
*/ 
function listFiles(auth) { 
    var service = google.drive('v3'); 
    service.files.list({ 
    auth: auth, 
    pageSize: 10, 
    fields: "nextPageToken, files(id, name)" 
    }, function(err, response) { 
    if (err) { 
     console.log('The API returned an error: ' + err); 
     return; 
    } 
    var files = response.files; 
    if (files.length == 0) { 
     console.log('No files found.'); 
    } else { 
     console.log('Files:'); 
     for (var i = 0; i < files.length; i++) { 
     var file = files[i]; 
     console.log('%s (%s)', file.name, file.id); 
     } 
    } 
    }); 
} 

OAuth 옵션 (다른 Gmail 계정보다 Gmail 계정)

또한 개발자 콘솔에는 이라는 제목의 URL을 허용 목록에 추가 할 수있는 권한이 부여 된 JavaScript 원본이 있습니다. 누구든지 AWS Lambda에서 콜 아웃을 만들 때 사용되는 URL을 알고 있습니까?

+0

당신은 스크립트에서 문자열로의 OAuth 토큰의 내용을 유지할 수 있습니다. –

답변

0

내부에 Google 자격 증명이있는 프록시 서비스가 필요합니다. 이렇게하면 사용자에게 인증을 요청할 필요가 없습니다. 프록시 서비스에 이미 액세스 할 자격 증명이 있습니다. 다음은 Google API에 프록시 연결에 사용하는 서비스입니다.

https://github.com/dnprock/gapiaccess

관련 문제