Wednesday 7 August 2013

Getting 401 uploading file into a table with a service account

Getting 401 uploading file into a table with a service account

I am using nodejs and the REST API to interact with bigquery. I am using
the google-oauth-jwt module for JWT signing.
I granted a service account write permission. So far I can list projects,
list datasets, create a table and delete a table. But when it comes to
upload a file via multipart POST, I ran into two problems:
gzipped json file doesn't work, I get an error saying "end boundary missing"
when I use uncompressed json file, I get a 401 unauthorized error
I don't think this is related to my machine's time being out of sync since
other REST api calls worked as expected.
var url = 'https://www.googleapis.com/upload/bigquery/v2/projects/' +
projectId + '/jobs';
var request = googleOauthJWT.requestWithJWT();
var jobResource = {
jobReference: {
projectId: projectId,
jobId: jobId
},
configuration: {
load: {
sourceFormat: 'NEWLINE_DELIMITED_JSON',
destinationTable: {
projectId: projectId,
datasetId: datasetId,
tableId: tableId
},
createDisposition: '',
writeDisposition: ''
}
}
};
request(
{
url: url,
method: 'POST',
jwt: jwtParams,
headers: {
'Content-Type': 'multipart/related'
},
qs: {
uploadType: 'multipart'
},
multipart: [
{
'Content-Type':'application/json; charset=UTF-8',
body: JSON.stringify(jobResource)
},
{
'Content-Type':'application/octet-stream',
body: fileBuffer.toString()
}
]
},
function(err, response, body) {
console.log(JSON.parse(body).selfLink);
}
);
Can anyone shine some light on this?
P.S. the documentation on bigquery REST api is not up to date on many
things, wish the google guys can keep it updated

No comments:

Post a Comment