.net - Using google drive sdk, unable to upload a file: Response coming to null -
i have simple code below upload google drive response = null everytime. authentication works , able list files not upload or create directory. need permissions? have seen samples , following them still upload doesn't work.
it of great if correct me. below upload code.
public static google.apis.drive.v2.data.file uploadfile(driveservice service, string uploadfile, string parent = null) { if (system.io.file.exists(uploadfile)) { google.apis.drive.v2.data.file body = new google.apis.drive.v2.data.file(); body.title = system.io.path.getfilename(uploadfile); body.description = "file uploaded installed app"; body.mimetype = getmimetype(uploadfile); //body.parents = new list<parentreference>() { new parentreference() { id = parent } }; // file's content byte[] bytearray = system.io.file.readallbytes(uploadfile); system.io.memorystream stream = new system.io.memorystream(bytearray); try { filesresource.insertmediaupload request = service.files.insert(body, stream, getmimetype(uploadfile)); //request.convert = true; request.upload(); google.apis.drive.v2.data.file f = request.responsebody; console.writeline(f.id); return f; } catch (exception ex) { console.writeline("an error occured: " + ex.message); return null; } } else { console.writeline("file not exist: " + uploadfile); return null; } } need guidance on this, appreciate if me out here!
uploading files google drive require use drive rest api. since have authentication token, you'll need call post url , other parameters needed set.
a simple upload case this
post /upload/drive/v2/files?uploadtype=media http/1.1 host: www.googleapis.com content-type: image/jpeg content-length: number_of_bytes_in_file authorization: bearer your_auth_token jpeg data there other uploadtypes choose from, pick benefit best. more information uploading files can seen @ v2 official documentation. sample code provided in github repository (though in js).
hope helps!
Comments
Post a Comment