Google Drive API javascript

后端 未结 3 1800
天涯浪人
天涯浪人 2020-12-03 08:25

I\'m trying to use the Google drive to list files.

Using the answer in https://stackoverflow.com/a/11280257 I found a problem that I can\'t discover the reason.

3条回答
  •  天涯浪人
    2020-12-03 08:53

    Code looks OK and you're correctly waiting until gapi.client.load completes. Might just be an error with loading the Drive JS files or some other issue (maybe bad JS file cached?). I modified your example a little bit to run on jsfiddle, take a look at http://jsfiddle.net/Rbg44/4/ for the full example:

    HTML:

    
    
    Files:

    JS:

    var CLIENT_ID = '...';
    var API_KEY = '...';
    var SCOPES = '...';
    
    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
        };
        gapi.auth.authorize(options, handleAuthResult);
        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

    Can you check in your browsers dev tools if there is any sort of issue in the request made when you call gapi.client.load()?

提交回复
热议问题