Matteo Stohlman
Matteo Stohlman

Reputation: 192

Google Apps Script and Google Fusion Tables. Invalid Credentials

I'm trying to add a row of data to a Fusion Table. Having trouble with OAuth it seems. I go through requesting for a token and receive one and store it. This is the code used to do this:

var AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth';

function getURLForAuth(){
 return AUTHORIZE_URL + '?response_type=code&client_id='+CLIENT_ID+'&redirect_uri='+REDIRECT_URL +'&scope=https://www.googleapis.com/auth/fusiontables&state=/profile';  
}

After allowing access on the google site page this code gives me a page from which I can take the token and store it. I then try to use this token to insert a few values into a table with this code:

 function _submitLog(e){
 var rowToAdd = [e.parameter.empName,e.parameter.jobName,e.parameter.taskName,e.parameter.hoursBox, e.parameter.date]; 
 runSQL("INSERT INTO "+TABLE_NAME +" (logNum,EmployeeName,JobName,TaskName,Hours,Date) VALUES (" +"'" +e.parameter.empName +"'" +"," +"'"+e.parameter.jobName +"'"+"," +"'"+e.parameter.taskName+"'" +"," +"'"+e.parameter.hoursBox+"'" +"," +"'"+e.parameter.date+"'" +")")  
}


 function runSQL(sql){

 var getDataURL = 'https://www.googleapis.com/fusiontables/v1/query?sql='+sql;
 var dataResponse = UrlFetchApp.fetch(getDataURL,getUrlFetchOptions()).getContentText(); 
 Logger.log(dataResponse);
 return dataResponse;
}


function getUrlFetchOptions() {
var token = UserProperties.getProperty(tokenPropertyName);
Logger.log(token);
return {
"method" : "post",
"ContentType" : "application/json",
"headers":{"Authorization" :"Bearer "+token,
           "Accept" :"application/json"}


 };
}

Im Sure I'm missing something extremely obvious, but I've been staring for hours and can't come up with it. Any help, even about best practices which I have probably obliterated would be great.

Upvotes: 0

Views: 787

Answers (2)

Matteo Stohlman
Matteo Stohlman

Reputation: 192

Click Here for a full example of the basic of POSTing to Fusion Tables API from Google apps script.

Upvotes: 1

Mogsdad
Mogsdad

Reputation: 45740

It does look like you're missing some details, but don't knock yourself out. Take a look at this excellent Fusion Tables Library, which includes a function doOAuth() to set up the authorization for your script without making you write it all yourself.

You need to set up a ScriptProperty with the token that you've obtained - the instructions are with the library. Then call doAuth() from the debugger (twice), and you're ready to go.

I should be getting royalties from James F for this!

Upvotes: 2

Related Questions