Secondary Google account (part6)

This commit is contained in:
benweet 2013-12-27 01:01:11 +01:00
parent 5f6c4b5450
commit 02640f1f89
2 changed files with 135 additions and 110 deletions

View File

@ -113,6 +113,8 @@ define([
authorizationMgrMap[accountId] = authorizationMgr;
}
task.onRun(function() {
var currentToken = gapi.auth.getToken();
var newToken;
function loadGdriveClient() {
if(gapi.client.drive) {
task.chain();
@ -126,7 +128,7 @@ define([
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo',
data: {
access_token: gapi.auth.getToken().access_token
access_token: newToken.access_token
},
timeout: constants.AJAX_TIMEOUT,
type: "GET"
@ -138,7 +140,7 @@ define([
else {
authorizationMgr.setUserId(data.user_id);
authorizationMgr.add(permission);
authorizationMgr.token = gapi.auth.getToken();
authorizationMgr.token = newToken;
task.chain(loadGdriveClient);
}
}).fail(function(jqXHR) {
@ -166,6 +168,8 @@ define([
immediate: immediate,
authuser: immediate === false ? '' : authuser
}, function(authResult) {
newToken = gapi.auth.getToken();
gapi.auth.setToken(currentToken);
if(!authResult || authResult.error) {
if(connected === true && immediate === true) {
// If immediate did not work retry without immediate
@ -199,7 +203,6 @@ define([
function startAuthenticate() {
immediate = true;
if(authorizationMgr.token && authorizationMgr.isAuthorized(permission)) {
gapi.auth.setToken(authorizationMgr.token);
task.chain();
return;
}
@ -220,6 +223,14 @@ define([
task.enqueue();
};
function runWithToken(accountId, functionToRun) {
var currentToken = gapi.auth.getToken();
var authorizationMgr = authorizationMgrMap[accountId];
gapi.auth.setToken(authorizationMgr.token);
functionToRun();
gapi.auth.setToken(currentToken);
}
googleHelper.upload = function(fileId, parentId, title, content, contentType, etag, accountId, callback) {
var result;
var task = new AsyncTask();
@ -274,6 +285,7 @@ define([
close_delim
].join("");
runWithToken(accountId, function() {
var request = gapi.client.request({
'path': path,
'method': method,
@ -306,6 +318,7 @@ define([
handleError(error, task);
});
});
});
task.onSuccess(function() {
callback(undefined, result);
});
@ -322,6 +335,7 @@ define([
authenticate(task, 'gdrive', accountId);
task.onRun(function() {
var body = {'title': title};
runWithToken(accountId, function() {
var request = gapi.client.drive.files.patch({
'fileId': fileId,
'resource': body
@ -343,6 +357,7 @@ define([
handleError(error, task);
});
});
});
task.onSuccess(function() {
callback(undefined, result);
});
@ -371,6 +386,7 @@ define([
}
];
}
runWithToken(accountId, function() {
var request = gapi.client.drive.files.insert({
'resource': metadata
});
@ -384,6 +400,7 @@ define([
handleError(response.error, task);
});
});
});
task.onSuccess(function() {
callback(undefined, result);
});
@ -402,6 +419,7 @@ define([
task.onRun(function() {
var nextPageToken;
function retrievePageOfChanges() {
runWithToken(accountId, function() {
var request;
if(nextPageToken === undefined) {
request = gapi.client.drive.changes.list({
@ -433,6 +451,7 @@ define([
task.chain();
}
});
});
}
task.chain(retrievePageOfChanges);
});
@ -460,9 +479,9 @@ define([
}
var id = ids[0];
var headers = {};
var token = gapi.auth.getToken();
if(token) {
headers.Authorization = "Bearer " + token.access_token;
var authorizationMgr = authorizationMgrMap[accountId];
if(authorizationMgr && authorizationMgr.token) {
headers.Authorization = "Bearer " + authorizationMgr.token.access_token;
}
$.ajax({
url: "https://www.googleapis.com/drive/v2/files/" + id,
@ -539,9 +558,9 @@ define([
return;
}
var headers = {};
var token = gapi.auth.getToken();
if(token) {
headers.Authorization = "Bearer " + token.access_token;
var authorizationMgr = authorizationMgrMap[accountId];
if(authorizationMgr && authorizationMgr.token) {
headers.Authorization = "Bearer " + authorizationMgr.token.access_token;
}
$.ajax({
url: file.downloadUrl,
@ -581,6 +600,8 @@ define([
connect(task);
authenticate(task, 'gdrive', accountId);
task.onRun(function() {
var authorizationMgr = authorizationMgrMap[accountId];
gapi.auth.setToken(authorizationMgr.token);
gapi.drive.realtime.load(fileId, function(result) {
// onFileLoaded
doc = result;
@ -604,10 +625,11 @@ define([
};
googleHelper.uploadImg = function(name, content, albumId, callback) {
var accountId = 'google.picasa0';
var result;
var task = new AsyncTask();
connect(task);
authenticate(task, 'picasa', 'google.picasa0');
authenticate(task, 'picasa', accountId);
task.onRun(function() {
var headers = {
"Slug": name
@ -621,9 +643,9 @@ define([
else if(name.match(/.gif$/i)) {
headers["Content-Type"] = "image/gif";
}
var token = gapi.auth.getToken();
if(token) {
headers.Authorization = "Bearer " + token.access_token;
var authorizationMgr = authorizationMgrMap[accountId];
if(authorizationMgr && authorizationMgr.token) {
headers.Authorization = "Bearer " + authorizationMgr.token.access_token;
}
$.ajax({
@ -742,6 +764,7 @@ define([
}
loadPicker(task);
task.onRun(function() {
var authorizationMgr = authorizationMgrMap[accountId];
var pickerBuilder = new google.picker.PickerBuilder();
pickerBuilder.setAppId(constants.GOOGLE_DRIVE_APP_ID);
var view;
@ -758,7 +781,7 @@ define([
pickerBuilder.enableFeature(google.picker.Feature.NAV_HIDDEN);
pickerBuilder.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
pickerBuilder.addView(view);
pickerBuilder.setOAuthToken(gapi.auth.getToken().access_token);
authorizationMgr && authorizationMgr.token && pickerBuilder.setOAuthToken(authorizationMgr.token.access_token);
}
else if(pickerType == 'folder') {
view = new google.picker.DocsView(google.picker.ViewId.FOLDERS);
@ -768,7 +791,7 @@ define([
view.setMimeTypes('application/vnd.google-apps.folder');
pickerBuilder.enableFeature(google.picker.Feature.NAV_HIDDEN);
pickerBuilder.addView(view);
pickerBuilder.setOAuthToken(gapi.auth.getToken().access_token);
authorizationMgr && authorizationMgr.token && pickerBuilder.setOAuthToken(authorizationMgr.token.access_token);
}
else if(pickerType == 'img') {
view = new google.picker.PhotosView();
@ -806,14 +829,15 @@ define([
};
googleHelper.uploadBlogger = function(blogUrl, blogId, postId, labelList, isDraft, publishDate, title, content, callback) {
var accountId = 'google.blogger0';
var task = new AsyncTask();
connect(task);
authenticate(task, 'blogger', 'google.blogger0');
authenticate(task, 'blogger', accountId);
task.onRun(function() {
var headers = {};
var token = gapi.auth.getToken();
if(token) {
headers.Authorization = "Bearer " + token.access_token;
var authorizationMgr = authorizationMgrMap[accountId];
if(authorizationMgr && authorizationMgr.token) {
headers.Authorization = "Bearer " + authorizationMgr.token.access_token;
}
function uploadPost() {
var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/";

View File

@ -199,7 +199,7 @@ define([
var localTitle = fileDesc.title;
// File deleted
if(change.deleted === true) {
eventMgr.onError('"' + localTitle + '" has been removed from Google Drive.');
eventMgr.onError('"' + localTitle + '" has been removed from ' + providerName + '.');
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
if(syncAttributes.isRealtime === true && fileMgr.currentFile === fileDesc) {
@ -226,13 +226,13 @@ define([
if(fileTitleChanged && remoteTitleChanged === true) {
fileDesc.title = file.title;
eventMgr.onTitleChanged(fileDesc);
eventMgr.onMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
eventMgr.onMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on ' + providerName + '.');
}
// If file content changed
if(!syncAttributes.isRealtime && fileContentChanged && remoteContentChanged === true) {
fileDesc.content = file.content;
eventMgr.onContentChanged(fileDesc);
eventMgr.onMessage('"' + file.title + '" has been updated from Google Drive.');
eventMgr.onMessage('"' + file.title + '" has been updated from ' + providerName + '.');
if(fileMgr.currentFile === fileDesc) {
fileMgr.selectFile(); // Refresh editor
}
@ -443,7 +443,7 @@ define([
googleHelper.refreshGdriveToken(accountId);
}
else if(err.type == "not_found") {
eventMgr.onError('"' + fileDesc.title + '" has been removed from Google Drive.');
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
gdriveProvider.stopRealtimeSync();
@ -482,10 +482,11 @@ define([
$('.submenu-sync-' + providerId).toggle(settings.gdriveMultiAccount > accountIndex);
// Create export dialog
document.querySelector('.modal-upload-' + providerId).innerHTML = _.template(dialogExportGdriveHTML, {
var modalUploadElt = document.querySelector('.modal-upload-' + providerId);
modalUploadElt && (modalUploadElt.innerHTML = _.template(dialogExportGdriveHTML, {
providerId: providerId,
providerName: providerName
});
}));
// Choose folder button in export modal
$('.export-' + providerId + '-choose-folder').click(function() {
@ -522,7 +523,7 @@ define([
syncLocations[syncAttributes.syncIndex] = syncAttributes;
var fileDesc = fileMgr.createFile(file.title, file.content, syncLocations);
fileMgr.selectFile(fileDesc);
eventMgr.onMessage('"' + file.title + '" created successfully on Google Drive.');
eventMgr.onMessage('"' + file.title + '" created successfully on ' + providerName + '.');
});
}
else if(state.action == "open") {