Following the example code from google drive quickstart I was able to recreate the sample in Dart. A lot of the heavy lifting is done by the js-interop cause of the dependency on google javascript client api. This solution is fine for now, I’d take a good guess the dart team will provide client apis at some point.
Started off by creating a new sample project dart_drive_quickstart, removed all default code and add js-interop.
Bring in the javascript client apis as ScriptElement and set a onload handler to a dart callback.
Manually adding the script code and hooking up a callback from javascript to dart seemed to work fine. I ran into issues with callbacks not being scoped when they should of been scoped. Another odd thing was the need for the call to setTimeout, the callback to handleAuthResult would not get fired if checkAuth was not called from setTimeout.
The rest of the code included was for the most part a direct translation of the quickstart sample. Added some dart flavoring when appropriate.
Here are some action shots and code. The full project can be found on github dart_drive_quickstart
import'dart:html';import'dart:json';import'package:js/js.dart'asjs;finalStringCLIENT_ID='299615367852.apps.googleusercontent.com';finalStringSCOPE='https://www.googleapis.com/auth/drive';voidmain(){js.scoped((){voidinsertFile(FilefileData,[callback=null]){Stringboundary='-------314159265358979323846';Stringdelimiter="\r\n--$boundary\r\n";Stringclose_delim="\r\n--$boundary--";varreader=newFileReader();reader.readAsBinaryString(fileData);reader.on.load.add((Evente){varcontentType=fileData.type;if(contentType.isEmpty){contentType='application/octet-stream';}varmetadata={'title':fileData.name,'mimeType':contentType};varbase64Data=window.btoa(reader.result);varsb=newStringBuffer();sb..add(delimiter)..add('Content-Type: application/json\r\n\r\n')..add(JSON.stringify(metadata))..add(delimiter)..add('Content-Type: ')..add(contentType)..add('\r\n')..add('Content-Transfer-Encoding: base64\r\n')..add('\r\n')..add(base64Data)..add(close_delim);varmultipartRequestBody=sb.toString();print("multipartRequestBody");print(multipartRequestBody);js.scoped((){varrequest=js.context.gapi.client.request(js.map({'path':'/upload/drive/v2/files','method':'POST','params':{'uploadType':'multipart'},'headers':{'Content-Type':'multipart/mixed; boundary="$boundary"'},'body':multipartRequestBody}));if(callback==null){callback=newjs.Callback.many((js.ProxyjsonResp,varrawResp){print(js.context.JSON.stringify(jsonResp));print(rawResp);Mapr=JSON.parse(js.context.JSON.stringify(jsonResp));StringBuffersb=newStringBuffer();if(r.containsKey('error')){sb.add(r.toString());}else{sb.add("${r["title"]} has been uploaded.");}query('#text').text=sb.toString();});}request.execute(callback);});});};voiduploadFile(Eventevt){js.scoped((){js.context.gapi.client.load('drive','v2',newjs.Callback.many((){varfile=evt.target.files[0];insertFile(file);}));});}js.context.handleAuthResult=newjs.Callback.many((js.ProxyauthResult){MapdartAuthResult=JSON.parse(js.context.JSON.stringify(authResult));print("dartAuthResult = ${dartAuthResult}");varauthButton=query('#authorizeButton');varfilePicker=query('#filePicker');authButton.style.display='none';filePicker.style.display='none';if(!dartAuthResult.containsKey('error')){// Access token has been successfully retrieved, requests can be sent to the API.filePicker.style.display='block';filePicker.on['change'].add(uploadFile);}else{authButton.style.display='block';authButton.on.click.add((Evente){js.scoped((){js.context.gapi.auth.authorize(js.map({'client_id':CLIENT_ID,'scope':SCOPE,'immediate':true}),js.context.handleAuthResult);});});}});js.context.handleClientLoad=newjs.Callback.many((){js.context.window.setTimeout(js.context.checkAuth,1);});js.context.checkAuth=newjs.Callback.many((){js.context.gapi.auth.authorize(js.map({'client_id':CLIENT_ID,'scope':SCOPE,'immediate':true}),js.context.handleAuthResult);});});ScriptElementscript=newScriptElement();script.src="http://apis.google.com/js/client.js?onload=handleClientLoad";script.type="text/javascript";document.body.children.add(script);}