Reputation: 695
I see there are several posts regarding "Cannot set Property" and I'm still struggling with pinpointing what I'm missing here. I'm new to Javascript and GAS. I've been a book on GAS called, "GScript enterprise application essentials". This code is from the book and I'm not having success w/ it working. It indicates to run it twice and at one point it did prompt for authorization, which only happened once and i've seen this error prior to that and since then. Can anyone suggest a resource or something I can consult to help figure it out.. or maybe understand the error better?? Thanks!!
CODE
/*
* Private for OAuth
* @ returns OAuth headers
*/
DOCS_LIST_API.googleOAuth = function() {
var oAuthConfig = UrlFetchApp.addOAuthService("google");
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken? scope=https://docs.google.com/feeds/");
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:'google', oAuthUseToken:"always"};
}
var DOCS_LIST_API = {};
/*
* @ args docID String the id for a Google Document
* @ args format String can be, "txt", "odt", "pdf", "html", "rtf", "doc", "png",
"zip"
* @ returns blob
*
*/
DOCS_LIST_API.GdocToFormat = function(docID, format){
var fetchArgs = DOCS_LIST_API.googleOAuth;
fetchArgs.headers = { "GData-Version": "3.0" };
fetchArgs.method = 'get';
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id='+
docID+'&exportFormat='+format+'&format='+format;
return UrlFetchApp.fetch(url, fetchArgs);
}
// Run it twice
function doOAuth(){
try{
DOCS_LIST_API.GdocToFormat();
}catch(e){
}
}
Upvotes: 0
Views: 1114
Reputation: 2496
You get an error because you try to set a property of DOCS_LIST_API
before declaring it as a variable.
Move var DOCS_LIST_API = {};
to the top.
Upvotes: 2