UNPKG

delta-store

Version:
118 lines 4.77 kB
/** * Created by Papa on 1/2/2016. */ "use strict"; var ApiConstants = (function () { function ApiConstants() { } return ApiConstants; }()); ApiConstants.URL_PREFIX = 'https://www.googleapis.com/auth/'; ApiConstants.DRIVE_PREFIX = 'drive.'; ApiConstants.PREFIX = ApiConstants.URL_PREFIX + ApiConstants.DRIVE_PREFIX; ApiConstants.APP_FOLDER = ApiConstants.PREFIX + 'appfolder'; ApiConstants.INSTALL = ApiConstants.PREFIX + 'install'; ApiConstants.FILE = ApiConstants.PREFIX + 'file'; ApiConstants.SHEETS = 'https://spreadsheets.google.com/feeds'; ApiConstants.PROFILE = 'profile'; ApiConstants.ALL_SCOPES = [ApiConstants.APP_FOLDER, ApiConstants.INSTALL, ApiConstants.FILE, ApiConstants.SHEETS, ApiConstants.PROFILE]; exports.ApiConstants = ApiConstants; var GoogleApi = (function () { function GoogleApi() { this.rootUrl = 'https://accounts.google.com/o/oauth2/auth'; this.redirectUri = 'http://localhost/callback'; } GoogleApi.prototype.mobileAuthenticate = function (clientId, apis, options, deferred) { var _this = this; if (options !== undefined) { if (options.hasOwnProperty('redirect_uri')) { this.redirectUri = options.redirect_uri; } } var url = this.getMobileAuthUrl(clientId, apis); var browserRef = window.open(url, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes'); var authenticatePromise = new Promise(function (resolve, reject) { browserRef.addEventListener('loadstart', function (event) { console.log('loadstart, event.url: ' + event.url); if ((event.url).indexOf(_this.redirectUri) === 0) { browserRef.removeEventListener('exit', function (event) { }); browserRef.close(); var callbackResponse = (event.url).split('#')[1]; var responseParameters = (callbackResponse).split('&'); var parameterMap = {}; for (var i = 0; i < responseParameters.length; i++) { var keyValue = responseParameters[i].split('='); parameterMap[keyValue[0]] = keyValue[1]; } if (parameterMap.access_token !== undefined && parameterMap.access_token !== null) { resolve(parameterMap); } else { reject('Problem authenticating'); } } }); browserRef.addEventListener('exit', function (event) { reject('The sign in flow was canceled'); }); }); return authenticatePromise; }; GoogleApi.prototype.getMobileAuthUrl = function (clientId, apis) { var url = this.rootUrl + "?client_id=" + clientId + "&redirect_uri=" + this.redirectUri + "&scope=" + apis.join(' ') + "&approval_prompt=force&response_type=token"; return url; }; GoogleApi.prototype.authorizeApis = function (apiKey, clientId, apis) { var initializeApiPromise = new Promise(function (resolve, reject) { var scopeCallbacks = []; try { if (apiKey) { gapi.client.setApiKey(apiKey); } gapi.auth.authorize({ client_id: clientId, scope: apis.join(' '), immediate: false }, function (authResult) { if (authResult && !authResult.error) { resolve(gapi); } else { reject(authResult.error); } }); } catch (error) { reject(error); } }); return initializeApiPromise; }; GoogleApi.prototype.loadApi = function (apiName, version) { return Promise.resolve().then(function () { if (version) { return gapi.client.load(apiName, version); } else { return gapi.load(apiName, undefined); } }); }; GoogleApi.prototype.request = function (path, method, params, headers, body) { return new Promise(function (resolve, reject) { resolve(); }).then(function () { return gapi.client.request({ path: path, method: method, params: params, headers: headers, body: body }); }); }; return GoogleApi; }()); exports.GoogleApi = GoogleApi; //# sourceMappingURL=GoogleApi.js.map