I followed all the steps mentioned in google drive sdk. I created a sample application on my device(android, running jelly bean) and am able to upload a file on to drive. Wh
I don't think we need any Access Token to download a file. I had the same problem, and this worked:
private class DownloadFile extends AsyncTask<Void, Long, Boolean> {
private com.google.api.services.drive.model.File driveFile;
private java.io.File file;
public DownloadFile(File driveFile) {
this.driveFile = driveFile;
}
@Override
protected Boolean doInBackground(Void... params) {
if (driveFile.getDownloadUrl() != null
&& driveFile.getDownloadUrl().length() > 0) {
try {
HttpResponse resp = mDriveService
.getRequestFactory()
.buildGetRequest(
new GenericUrl(driveFile.getDownloadUrl()))
.execute();
OutputStream os = new FileOutputStream(file);
CopyStream(resp.getContent(), os);
os.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
//use the file
}
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
I have answered this question, and all related Drive on Android questions, over here:
Android Open and Save files to/from Google Drive SDK
In that answer, I posted code for a method that I used to download files from Google Drive (if the following code by itself isn't clear, have a look at the complete answer that I linked to.)
private java.io.File downloadGFileToJFolder(Drive drive, String token, File gFile, java.io.File jFolder) throws IOException {
if (gFile.getDownloadUrl() != null && gFile.getDownloadUrl().length() > 0 ) {
if (jFolder == null) {
jFolder = Environment.getExternalStorageDirectory();
jFolder.mkdirs();
}
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(gFile.getDownloadUrl());
get.setHeader("Authorization", "Bearer " + token);
HttpResponse response = client.execute(get);
InputStream inputStream = response.getEntity().getContent();
jFolder.mkdirs();
java.io.File jFile = new java.io.File(jFolder.getAbsolutePath() + "/" + getGFileName(gFile)); // getGFileName() is my own method... it just grabs originalFilename if it exists or title if it doesn't.
FileOutputStream fileStream = new FileOutputStream(jFile);
byte buffer[] = new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0) {
fileStream.write(buffer, 0, length);
}
fileStream.close();
inputStream.close();
return jFile;
} catch (IOException e) {
// Handle IOExceptions here...
return null;
}
} else {
// Handle the case where the file on Google Drive has no length here.
return null;
}
}
This is a known issue that will be resolved with the release of the Google Play Services APIs.
Since your application is authorized for the https://www.googleapis.com/auth/drive.file
scope, and the download endpoint doesn't support the ?key=
query parameter, there is no way for our server to know which project is issuing the request (to make sure the app has authorization to read this file's content).
In the meantime, the only workaround I can recommend is using the broad scope: https://www.googleapis.com/auth/drive
. Please use only that while developing your application and waiting for the Google Play Services to be released.
To learn more about how you will be able to use the new authorization APIs in Android, you might be interested in those 2 Google I/O talks: Building Android Applications that Use Web APIs and Writing Efficient Drive Apps for Android