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. When trying to download the same file, I am able to get the meta data like fileID, fileTitle, fileDownloadURL etc but not able to download the content. I get 401 Unauthorized error.
My app AUTH SCOPE is AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive.file";
I am doing the following to get the OAUTH Token:
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
am.getAuthToken(
mAccount,
AUTH_TOKEN_TYPE,
options,
this,
new OnTokenAcquired(),
new Handler(){
@Override
public void handleMessage(Message msg) {
invadiateToken();
super.handleMessage(msg);
}
});
Based on the token this is how I build the Drive object
Drive buildService(final String AuthToken) {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey(API_KEY);
driveRequest.setOauthToken(AuthToken);
}
});
return b.build();
}
I am able to upload the file using the following code:
private void uploadLocalFileToDrive(Drive service) throws IOException{
// File's metadata.
String mimeType = "text/plain";
File body = new File();
body.setTitle("myText.txt");
body.setDescription("sample app by varun");
body.setMimeType("text/plain");
// File's content.
java.io.File fileContent = new java.io.File(mInternalFilePath);
FileContent mediaContent = new FileContent(mimeType, fileContent);
service.files().insert(body, mediaContent).execute();
}
While trying to download the same file uploaded by this app, I get a 401 unauthorized error at this line HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute()
from the following code snippet
private void downloadFileFromDrive(Drive service) throws IOException {
Files.List request;
request = service.files().list();
do {
FileList files = request.execute();
for(File file:files.getItems()){
String fieldId = file.getId();
String title = file.getTitle();
Log.e("MS", "MSV:: Title-->"+title+" FieldID-->"+fieldId+" DownloadURL-->"+file.getDownloadUrl());
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0 ) {
GenericUrl url = new GenericUrl(file.getDownloadUrl());
HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute();
InputStream isd = resp.getContent();
Log.e("MS", "MSV:: FileOutPutStream--->"+getFilesDir().getAbsolutePath()+"/downloaded.txt");
} else {
Log.e("MS", "MSV:: downloadURL for this file is null");
}
}
request.setPageToken(files.getNextPageToken());
} while (request.getPageToken()!=null && request.getPageToken().length()>0);
}
Can anyone help me out and let me know what I am doing wrong???