【问题标题】:Uncaught ReferenceError: gapi is not defined at makeApiCall?未捕获的 ReferenceError:gapi 未在 makeApiCall 中定义?
【发布时间】:2021-06-20 23:48:42
【问题描述】:

 function makeApiCall(){   
   
 gapi.client.load('calendar', 'v3', function () { 
        
                 var request = gapi.client.calendar.events.insert
                 console.log(request);
                 ({
                    'calendarId': '', 
                    "resource": resource    
                });  
     
 });
}

【问题讨论】:

    标签: javascript google-calendar-api google-api-js-client


    【解决方案1】:

    除非您忘记发布部分代码,否则您可能忘记注册库以及授权用户。

    <script src="https://apis.google.com/js/client:platform.js"></script>
    

    使用 Google 登录的完整示例

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Hello cal v3 </title>
        <!-- Create a client id on Google developer console. Web credentials https://youtu.be/pBVAyU4pZOU  -->
        <meta name="google-signin-client_id"
              content="YourClientId">
        <meta name="google-signin-scope" content="https://www.googleapis.com/auth/calendar">
    </head>
    <body>
    
    <h1>Hello cal v3 </h1>
    
    <!-- The Sign-in button. This will run `getFileAsync()` on success. -->
    <p class="g-signin2" data-onsuccess="makeacall"></p>
    
    <!-- The API response will be printed here. -->
    <textarea cols="80" rows="20" id="query-output"></textarea>
    
    <script>
    
        // Query the API and print the results to the page.
        async function makeacall() {
    
            try {
                await gapi.client.load('calendar', 'v3');
                const response = gapi.client.calendar.events.insert({
                    calendarId: 'XXX', 
                    resource: resource
                });
             displayResults(response)
                
            } catch (e) {
                console.error('Error getting files', e)
            }
        }
    
     function displayResults(response) {
        var formattedJson = JSON.stringify(response.result, null, 2);
        document.getElementById('query-output').value = formattedJson;
    }
    
    
    </script>
    
    <!-- Load the JavaScript API client and Sign-in library.   -->
    <script src="https://apis.google.com/js/client:platform.js"></script>
    

    重定向 uri 不匹配 JavaScript 源错误

    确保您已在 Google 开发者控制台上正确配置了 Web 客户端并添加了正确的 javascript 源。 Simple solution for redirect_uri_mismatch error with JavaScript

    【讨论】:

    • 请求中的 JavaScript 源 localhost:9001 与授权给 OAuth 客户端的源不匹配。访问 console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number} 以更新授权的 JavaScript 来源。
    • 点击登录按钮后是这样的
    • 您需要在 Google 开发者控制台中为您的 Web 客户端正确设置 Javascript 源。 youtu.be/V0-4LnHwFho
    • 开发者没有给你访问这个应用程序的权限。它目前正在测试中,尚未经过 Google 验证。如果您认为您应该有权访问,请联系开发人员 (venkatasubbareddy.singamala@gmail.com)。
    • 在提供 gmail 和密码后,它的显示就像。我看过你撕的视频,很有用。
    【解决方案2】:

    你需要在页面加载时初始化 gapi

    gapi.auth2.init({
       client_id: 'xxxxxxxxxxx'
    });
    

    【讨论】:

    • 你加了吗&lt;script src="https://apis.google.com/js/plus.js?onload=init"&gt;&lt;/script&gt;
    • 未捕获的类型错误:无法在 HTMLButtonElement.onclick 的 makeApiCall (calendar.html:83) 处读取未定义的属性“init”
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2020-08-28
    • 2019-11-08
    • 2021-09-09
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多