在Meteor中加载Google JavaScript客户端库

时间:2015-12-08 13:38:59

标签: javascript meteor

我正在尝试加载Google客户端库以在我的Meteor应用程序中使用Google日历,但我的回调(onload=handleClientLoad)功能未执行。使用简单的HTML + JavaScript应用程序时也一样。我还在Google授权网址中注册了我的Meteor应用程序网址localhost:3000

Template.hello.events({
    'click button': function () {
        callGoogle();
    }
});


function callGoogle() {
    jQuery.ajax({
        url: 'https://apis.google.com/js/client.js?onload=handleClientLoad',
        dataType: 'script',
        success: function () {
            console.log("Success");

        },
        error: function (e) {
            console.log("Error")
        },
        async: true
    });

    return false;
}

//This function is not executing
function handleClientLoad() {
    console.log("handleClientLoad");

    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 2);
}

function checkAuth() {
    gapi.auth.authorize({
        client_id: clientId,
        scope: scopes,
        immediate: false
    }, handleAuthResult);
}

function handleAuthResult(authResult) {

    console.log("authResult", authResult);
    if (authResult && !authResult.error) {
        gapi.client.load('calendar', 'v3', listUpcomingEvents);
    }
}

function listUpcomingEvents() {
    var request = gapi.client.calendar.events.list({
        'calendarId': 'primary',
        'timeMin': (new Date()).toISOString(),
        'showDeleted': false,
        'singleEvents': true,
        'maxResults': 10,
        'orderBy': 'startTime'
    });

2 个答案:

答案 0 :(得分:4)

您需要将您的功能导出到全局范围:

handleClientLoad = function() {
  console.log("handleClientLoad");
  gapi.client.setApiKey(apiKey);
  window.setTimeout(checkAuth, 2);
};

答案 1 :(得分:0)

Just Add reference to google API in main HTML
<script src="https://apis.google.com/js/client.js"> </script>

以后每当你想使用gapi对象时   你只需要设置如下所示的身份验证参数

var user=Meteor.user();
gapi.auth.setToken({
                        access_token: user.services.google.accessToken
                });
//Once you have set token to gapi, load calendar and on success do insert, read, edit etc

var ret = gapi.client.load('calendar', 'v3', function (error, result) {

                 }

`