0

I am creating a Google Drive Api site to get the comments from a Google doc. Every time I make a request I get: Uncaught TypeError: Cannot read property 'comments' of undefined. Here is my code:

<Html>
<head>
    <title>test</title>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script src="https://apis.google.com/js/client.js"></script>
    <script>
        function auth() {
            var config = {
                'client_id': '<YOUR_CLIENT_ID>',
                'scope': 'https://www.googleapis.com/auth/urlshortener'
            };
            gapi.auth.authorize(config, function () {
                console.log('login complete');
                console.log(gapi.auth.getToken());
        });

        retrieveComments();


    }



    function retrieveComments() {

        printComment('<fileid>');
    }
    function printComment(fileId) {
        var request = gapi.client.drive.comments.list({
            'fileId': fileId
        });
        request.execute(function (resp) {
            if (!resp.error) {
                console.log('Modified Date: ' + resp.modifiedDate);


                console.log('Content: ' + resp.content);
            } else {
                console.log('Error code: ' + resp.error.code);
                console.log('Error message: ' + resp.error.message);
                // More error information can be retrieved with resp.error.errors.
            }

        });
    }


</script>



</head>
<body>
    <button onclick="auth();">Authorize</button>
</body>
</Html>    

Please help! I am about to pull hair out.

Dayton Wang
  • 2,332
  • 1
  • 12
  • 17
backburn
  • 1
  • 1
  • Using `gapi.client.request()` instead of `gapi.client.drive.comments.list()`. See the related issue: http://stackoverflow.com/questions/11315962/google-drive-api-javascript – Dayton Wang Feb 20 '15 at 01:08

1 Answers1

0

I found the issue with the code. I Authorized the script but never called gapi.client.load('drive', 'v2', Callback)

<html>
<head>
<title>test</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script src="https://apis.google.com/js/client.js"></script>
<script>
    function auth() {
        var config = {
            'client_id': '<YOUR_CLIENT_ID>',
            'scope': 'https://www.googleapis.com/auth/urlshortener'
        };
        gapi.auth.authorize(config, function () {
            console.log('login complete');
            console.log(gapi.auth.getToken());
    });

        gapi.client.load('drive', 'v2', retrieveComments());




}

function retrieveComments() {

    printComment('<fileid>');
}
function printComment(fileId) {
    var request = gapi.client.drive.comments.list({
        'fileId': fileId
    });
    request.execute(function (resp) {
        if (!resp.error) {
            console.log('Modified Date: ' + resp.modifiedDate);


            console.log('Content: ' + resp.content);
        } else {
            console.log('Error code: ' + resp.error.code);
            console.log('Error message: ' + resp.error.message);
            // More error information can be retrieved with      resp.error.errors.
        }

    });
}


</script>



</head>
<body>
    <button onclick="auth();">Authorize</button>
</body>
</html>    

This is now working!

backburn
  • 1
  • 1