处理多个Gmail帐户

时间:2019-04-24 11:54:09

标签: javascript google-calendar-api

我正在使用Google Calendar JavaScript API。该功能仅适用于单个帐户。问题是当用户登录到我的两个gmail帐户时,它每次都要求用户选择用户要使用的帐户。

我希望API至少在会话中或在用户断开连接之前记住我的最后选择。

她是我的JS文件。

var clientId = $("#CalendarClientId").val();


var scopes = 'https://www.googleapis.com/auth/calendar';
var objEventList = [];
var calendarIdAtt;

function InitilizeCalendarAPI() {
    // Step 2: Reference the API key
    window.setTimeout(checkAuth, 5);
}
function CheckGoogleCalAuth() {
    
    gapi.auth.authorize({ client_id: clientId, scope: scopes }, HandleAuthCallBack);
}
function checkAuth() {
    gapi.auth.authorize({ client_id: clientId, scope: scopes }, HandelCalAuthResult);
}
function CheckAuthentication() {
    gapi.auth.checkSessionState({ client_id: clientId }, CheckAuthCallback);
}
function CheckAuthCallback(loginDataCallback) {
    
    if (loginDataCallback == false) {
        
        checkAuth();
        $("#googleAuthorize-Button").hide();
    }
}
function HandelCalAuthResult(authResult) {
    if (authResult && !authResult.error) {
        /*authorizeButton.style.visibility = 'hidden';*/
        $("#googleAuthorize-Button").hide();
        if ($("#googleConnected").length > 0) {
            $("#googleConnected").show();
        }
        /*bind listing of google calendar name in addevent page*/
        if ($("#divGoogleCalendarNameList").length > 0) {
            GetListOfCalendarNameFromGoogle();
        }
    } else {
        $("#googleAuthorize-Button").show();
        /*authorizeButton.style.visibility = '';*/
        if ($("#googleConnected").length > 0) {
            $("#googleConnected").hide();
        }
    }
    if ($('#googleAuthorized-Button').length > 0) {
        $("#googleAuthorize-Button").hide();
        $('#googleAuthorized-Button').show();
        $('#calenderSettingIcon').attr('data-original-title', 'Connected To Google');
    }
}

function CheckAutheForEventList() {
    
    gapi.auth.checkSessionState({ client_id: clientId }, CheckAuthCallbackListEvent);
}

function CheckAuthCallbackListEvent(loginDataCallback)
{
     gapi.auth.authorize({ client_id: clientId, scope: scopes }, LoadEventListinAPI);
}

function checkAuthEventList() {
    ConfirmConnectionToGoogleCalendar(function (isConnect) {        
            if (isConnect) {
                gapi.auth.authorize({ client_id: clientId, scope: scopes },             LoadEventListinAPI);
            }
        });
}

function LoadEventListinAPI() {
    
    gapi.client.load('calendar', 'v3', GetAllCalendar);
}

function GetAllCalendar() {
    gapi.client.load('calendar', 'v3', GetCalendarAndTheirEvents);
}

function GetCalendarAndTheirEvents() {
    
    objEventList = [];
    try {        
        var objDate = GetMinAndMaxDate();
        var finalMinDate = (objDate.MinDate).toISOString();;
        var finalMaxDate = (objDate.MaxDate).toISOString();;

        var request = gapi.client.calendar.calendarList.list();        
        var tempCount = 0;
        request.execute(function (resp) {
            
            var calLength = resp.items.length;
            $.each(resp.items, function (index, item) {
                if (item.accessRole == "owner") {
                    try {
                        calendarIdAtt = item.id;
                        var request = gapi.client.calendar.events.list({
                            'calendarId': item.id,
                            'showDeleted': false,
                            'singleEvents': true,
                            'orderBy': 'startTime',
                            'maxResults': 100,
                            'timeMin': finalMinDate,
                            'timeMax': finalMaxDate
                        });

                        request.execute(function (resp) {
                            tempCount = tempCount + 1;
                            var events = resp.items;
                            if (events.length > 0) {
                                objEventList.push(events);
                            }
                            if (tempCount == calLength) {
                                //Binding Kendo Scheduler with Google Calenda Events
                                var eventsList = BindEventToCalendar();
                                LoadEventsScheduler(eventsList);
                            }
                        });
                    }
                    catch (error) {
                        tempCount = tempCount + 1;
                        if (tempCount == calLength) {
                            BindEventToCalendar();
                        }
                    }
                }
                else {
                    tempCount = tempCount + 1;
                    if (tempCount == calLength) {
                        BindEventToCalendar();
                    }
                }
            });
        });
        return objEventList;
    }
    catch (exception) {
        BindEventToCalendar();
        return null;
    }
}

//Function will display google calendar events in the Kendo Scheduler
function LoadEventsScheduler(eventsList) {
    
    //This will retrun all the google calendar events to the server side for the scheduler
    var request = {
        EventsList: eventsList, CalendarAccount: calendarIdAtt
    };
    $.ajax({
        type: "POST",
        data: JSON.stringify(request),
        url: $_SetEventScheduler,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var data;
            if (msg.hasOwnProperty("d")) {
                data = msg.d;
            }
            else
                data = msg;
            if (data.success) {
                $("#scheduler").data("kendoScheduler").dataSource.read();
            }
            else {

            }
        }
    });
}

我注意到api显示我已登录,但是在有多个帐户的情况下日历未保存,因此它再次要求我选择该帐户。

摘要::我想保存完整的方案(可以是calendar_id或任何唯一的标识符),该方案可以告诉API应该选择哪个帐户和日历。

如果您需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

我只是使用了auth2实现,它运行良好。 基本上,当前的会话状态功能在旧的API中无法正常运行。这就是为什么它每次都要求选择帐户和日历的原因。