Skip to content

Instantly share code, notes, and snippets.

@adam-singer
Last active December 11, 2015 10:38
Show Gist options
  • Save adam-singer/4588427 to your computer and use it in GitHub Desktop.
Save adam-singer/4588427 to your computer and use it in GitHub Desktop.
Get a file with google drive api and dart
Your best bet is to use the `drive_v2_api_client`.
This can be done by setting up your google console apis with the following settings
```
Redirect URIs: http://127.0.0.1:3030/oauth2callback
JavaScript origins: http://127.0.0.1:3030
```
Then use the dart drive package `drive_v2_api_client` by putting the following depenencies in your pubspec.yaml file.
```
dependencies:
drive_v2_api_client:
git: git://github.com/Scarygami/dart_drive_v2_api_client.git
```
This example allows the client to get the fileid after the client has authenticated with the client id.
```
import 'dart:html';
import 'dart:json';
import "package:drive_v2_api_client/drive_v2_api_client.dart" as drivelib;
import "package:google_oauth2_client/google_oauth2_client.dart";
final CLIENT_ID = "<YOUR CLIENT ID FROM CONSOLE API>";
final SCOPES = [drivelib.Drive.DRIVE_FILE_SCOPE];
void main() {
var fileid = "1B_cGCNFjnK3dDriTMLsSS_zExfGFkQeewb3dcP4xSPg";
var auth = new OAuth2(CLIENT_ID, SCOPES);
var drive = new drivelib.Drive(auth);
drive.makeAuthRequests = true;
var loginButton = query("#login");
var output = query("#text");
loginButton.on.click.add((Event e) {
auth.login().then((token) {
output.appendHtml("Got Token ${token.type} ${token.data}<br><br>");
drive.files.get(fileid)
..then((data) {
output.appendHtml(data.toString());
});
});
});
}
```
You can fine all the source code here https://gist.github.com/4588427
body {
background-color: #F8F8F8;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
font-weight: normal;
line-height: 1.2em;
margin: 15px;
}
p {
color: #333;
}
#container {
width: 100%;
position: relative;
border: 1px solid #ccc;
background-color: #fff;
}
#text {
font-size: 9pt;
text-align: left;
}
import 'dart:html';
import 'dart:json';
import "package:drive_v2_api_client/drive_v2_api_client.dart" as drivelib;
import "package:google_oauth2_client/google_oauth2_client.dart";
final CLIENT_ID = "<YOUR CLIENT ID FROM CONSOLE API>";
final SCOPES = [drivelib.Drive.DRIVE_FILE_SCOPE];
String formatJson(String json) {
json = json.replaceAll("\":", "\": ");
json = json.replaceAll(",\"", ",\n\"");
json = json.replaceAll("\": {", "\":\n{");
return json;
}
void main() {
var fileid = "1B_cGCNFjnK3dDriTMLsSS_zExfGFkQeewb3dcP4xSPg";
var auth = new OAuth2(CLIENT_ID, SCOPES);
var drive = new drivelib.Drive(auth);
drive.makeAuthRequests = true;
var loginButton = query("#login");
var output = query("#text");
loginButton.on.click.add((Event e) {
auth.login().then((token) {
output.appendHtml("Got Token ${token.type} ${token.data}<br><br>");
drive.files.get(fileid)
..then((data) {
output.appendHtml("Got $fileid<br><br>");
output.appendHtml(formatJson(data.toString()));
});
});
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GetFile</title>
<link rel="stylesheet" href="getFile.css">
</head>
<body>
<button id="login">Login</button>
<div id="container">
<p id="text"></p>
</div>
<script type="application/dart" src="getFile.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
name: getFile
description: A sample application
dependencies:
browser: any
drive_v2_api_client:
git: git://github.com/Scarygami/dart_drive_v2_api_client.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment