theoephraim / node-google-spreadsheet

Google Sheets API wrapper for Javascript / Typescript
https://theoephraim.github.io/node-google-spreadsheet
The Unlicense
2.33k stars 390 forks source link

Attempt to access doc using JWT fails in TypeError #53

Closed Meandmybadself closed 9 years ago

Meandmybadself commented 9 years ago

Howdy.

Trying to access a private document using the Service Account method. Have verified that the document is shared with the email address associated with the defined service account.

This is using node v.4.0.0 on OSX. Tried using earlier versions of node to no luck.

Doing so nets me the following error:

/private/var/www/project-name/node_modules/google-spreadsheet/index.js:102
        if (google_auth.expires > +new Date()) return step();
                       ^
TypeError: Cannot read property 'expires' of undefined
    at async.series.auth (/private/var/www/project-name/node_modules/google-spreadsheet/index.js:102:24)
    at /private/var/www/project-name/node_modules/google-spreadsheet/node_modules/async/lib/async.js:689:13
    at iterate (/private/var/www/project-name/node_modules/google-spreadsheet/node_modules/async/lib/async.js:265:13)
    at async.forEachOfSeries.async.eachOfSeries (/private/var/www/project-name/node_modules/google-spreadsheet/node_modules/async/lib/async.js:284:9)
    at _parallel (/private/var/www/project-name/node_modules/google-spreadsheet/node_modules/async/lib/async.js:688:9)
    at Object.async.series (/private/var/www/project-name/node_modules/google-spreadsheet/node_modules/async/lib/async.js:710:9)
    at makeFeedRequest (/private/var/www/project-name/node_modules/google-spreadsheet/index.js:98:11)
    at getInfo (/private/var/www/project-name/node_modules/google-spreadsheet/index.js:157:10)
    at Object.<anonymous> (/private/var/www/project-name/server.js:25:4)
    at Module._compile (module.js:434:26)

Code requesting the spreadsheet is:

AppName.prototype._getGoogDoc           = function() {
    var creds   = this._settings['google-docs']['creds'];
    var gd      = new GoogleSpreadsheet(this._settings['google-docs']['key']);

    gd.useServiceAccountAuth(creds, function(err,sheet_info) {
        if (err) {
            console.log('error', err);
        } else {
            console.log('got it.', sheet_info);
        }

    });
    return gd;
};

Apologies if I'm doing something blatantly wrong here.

theoephraim commented 9 years ago

likely an issue with your callback! the sheet isnt ready to use until the callback completes (where you are writing console.log yet you are returning gd (and im assuming not waiting for it to be done) when you are then trying to use it.

Check out https://github.com/caolan/async it can help with managing callbacks and "control flow"

Meandmybadself commented 9 years ago

That's exactly the problem. Was attempting access before the auth callback was complete.

My bad. Thanks for the comment & all your work, sir.