来自javascript的google api:未捕获的TypeError:无法读取未定义的属性“授权”

时间:2019-02-03 14:21:23

标签: javascript

我正在尝试通过javascript页面将图像上传到Google驱动器。但是在授权步骤中,我收到此错误:

  

未捕获的TypeError:无法读取未定义的属性“授权”

错误来自此方法:

gapi.auth.authorize(options, handleAuthResult);

在我完整的代码下面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="https://apis.google.com/js/api.js"></script>      
    </head>
<body>
<button id="authorize-button">Start</button>
<div id="content">Files:</div>
<script>

var CLIENT_ID = 'XXXXXXXXXX.apps.googleusercontent.com';
var API_KEY = 'XXXXXXXX950HQ6hd6HS4';
var SCOPES = 'https://www.googleapis.com/auth/drive';

function handleClientLoad() {
    gapi.client.setApiKey(API_KEY);
    window.setTimeout(checkAuth,1);
}

function checkAuth() {
    var options = {
        client_id: CLIENT_ID,
        scope: SCOPES,
        immediate: true
    };
    gapi.auth.authorize(options, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');

    if (authResult && !authResult.error) {
        authorizeButton.style.visibility = 'hidden';
        makeApiCall();
    } else {
        authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    var options = {
        client_id: CLIENT_ID,
        scope: SCOPES,
        immediate: false
    };
    alert('pre autorization');
    gapi.auth.authorize(options, handleAuthResult);
    alert('post autorization');
    return false;
}

function makeApiCall() {  
    gapi.client.load('drive', 'v2', makeRequest);   
}

function makeRequest() {
    var request = gapi.client.drive.files.list({'maxResults': 5 });
    request.execute(function(resp) {          
        for (i=0; i<resp.items.length; i++) {
            var titulo = resp.items[i].title;
            var fechaUpd = resp.items[i].modifiedDate;
            var userUpd = resp.items[i].lastModifyingUserName;
            var userEmbed = resp.items[i].embedLink;
            var userAltLink = resp.items[i].alternateLink;

            var fileInfo = document.createElement('li');
            fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd ));                
            document.getElementById('content').appendChild(fileInfo);
        }
    });    
}

$(document).ready(function() {
  $('#authorize-button').on('click', handleAuthClick);
  $.getScript('//apis.google.com/js/api.js', function() {
    gapi.load('auth:client', handleClientLoad);
  });
});
</script>
</body>
</html>

0 个答案:

没有答案