node.js - Getting 401 uploading file into a table with a service account -
i using nodejs , rest api interact bigquery. using google-oauth-jwt module jwt signing.
i granted service account write permission. far can list projects, list datasets, create table , delete table. when comes upload file via multipart post, ran 2 problems:
- gzipped json file doesn't work, error saying "end boundary missing"
- when use uncompressed json file, 401 unauthorized error
i don't think related machine's time being out of sync since other rest api calls worked 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 shine light on this?
p.s. documentation on bigquery rest api not date on many things, wish google guys can keep updated
update 1:
here full http request:
post /upload/bigquery/v2/projects/239525534299/jobs?uploadtype=multipart http/1.1 content-type: multipart/related; boundary=71e00bd1-1c17-4892-8784-2facc6998699 authorization: bearer ya29.ahes6zryyfsupqz7xt-xwegufelmcvwi0rl3zthdwc4vnbi host: www.googleapis.com content-length: 876 connection: keep-alive --71e00bd1-1c17-4892-8784-2facc6998699 content-type: application/json {"jobreference":{"projectid":"239525534299","jobid":"test-upload-2013-08-07_2300"},"configuration":{"load":{"sourceformat":"newline_delimited_json","destinationtable":{"projectid":"239525534299","datasetid":"performance","tableid":"test_table"},"createdisposition":"create_never","writedisposition":"write_append"}}} --71e00bd1-1c17-4892-8784-2facc6998699 content-type: application/octet-stream {"practiceid":2,"fancount":5,"mvp":"hello"} {"practiceid":3,"fancount":33,"mvp":"hello"} {"practiceid":4,"fancount":71,"mvp":"hello"} {"practiceid":5,"fancount":93,"mvp":"hello"} {"practiceid":6,"fancount":92,"mvp":"hello"} {"practiceid":7,"fancount":74,"mvp":"hello"} {"practiceid":8,"fancount":100,"mvp":"hello"} {"practiceid":9,"fancount":27,"mvp":"hello"} --71e00bd1-1c17-4892-8784-2facc6998699--
you sending duplicate content-type
headers google api.
i don't have capability effortlessly make request google bigquery test, i'd start removing headers
property of options object request()
.
remove this:
headers: { 'content-type': 'multipart/related' },
the node.js request
module automatically detects have passed in multipart array, , adds appropriate content-type header. if provide own content-type header, end "duplicate" one, not contain multipart boundary.
if modify code print out actual headers sent:
var req = request({...}, function(..) {...}); console.log(req.headers);
you should see original code above (i'm using node repl):
> req.headers { 'content-type': 'multipart/related', 'content-type': 'multipart/related; boundary=af5ed508-5655-48e4-b43c-ae5be91b5ae9', 'content-length': 271 }
and following if remove explicit headers
option:
> req.headers { 'content-type': 'multipart/related; boundary=49d2371f-1baf-4526-b140-0d4d3f80bb75', 'content-length': 271 }
some servers don't deal multiple headers having same name. solves end boundary missing error api!
Comments
Post a Comment