简体   繁体   English

在Meteor中加载Google JavaScript客户端库

[英]Load Google JavaScript Client library in Meteor

I am trying to load the Google Client Library to use Google Calendar in my Meteor application but my callback ( onload=handleClientLoad ) function is not executing. 我正在尝试加载Google客户端库以在Meteor应用程序中使用Google日历,但是我的回调( onload=handleClientLoad )函数未执行。 The same is working when using from simple HTML +JavaScript app. 从简单的HTML + JavaScript应用程序使用时,该方法同样有效。 I have also registered my Meteor application URL localhost:3000 in Google authorize URLs. 我还在Google授权URL中注册了Meteor应用程序URL 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'
    });

You need to export your function to the global scope : 您需要将函数导出到全局范围:

handleClientLoad = function() {
  console.log("handleClientLoad");
  gapi.client.setApiKey(apiKey);
  window.setTimeout(checkAuth, 2);
};
Just Add reference to google API in main HTML
<script src="https://apis.google.com/js/client.js"> </script>

Later on whenever you want to use gapi object you just need to set authentication parameters like below 以后,每当您要使用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) {

                 }

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Google API Javascript客户端库加载到Chrome应用中 - How to Load Google API Javascript Client Library into Chrome App 无法将JavaScript库加载到Meteor应用程序中 - Unable to load javascript library into Meteor app 加载Google Javascript客户端库和自定义端点客户端库的正确方法是什么 - What is the Proper way to load the Google Javascript Client Library and a custom Endpoint Client Library Google Maps Javascript API v3将不会加载到meteor项目net :: ERR_BLOCKED_BY_CLIENT中 - Google Maps Javascript API v3 won't load in meteor project, net::ERR_BLOCKED_BY_CLIENT 使用Google的客户端库使用Javascript批量请求 - Batch requests with Google's Client Library with Javascript JavaScript中的Google API客户端库无法在本地运行 - Google API client library in javascript not working locally 是否无法在 Google colab 单元中加载 javascript 库? - Is it impossible to load a javascript library in a Google colab cell? 使用JavaScript的Google API客户端库访问Google Sites API - Using Google APIs Client Library for JavaScript to access Google Sites API Javascript-使用Google App Engine时如何加载JavaScript库? - Javascript - how to load the javascript library while using Google App Engine? 流星客户端javascript中的全局变量 - Global Variable in meteor client javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM