Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to download file created by my app on Google Drive, But can get the metadata of that file
    primarykey
    data
    text
    <p>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.</p> <p>My app AUTH SCOPE is AUTH_TOKEN_TYPE = "oauth2:<a href="https://www.googleapis.com/auth/drive.file" rel="nofollow">https://www.googleapis.com/auth/drive.file</a>";</p> <p>I am doing the following to get the OAUTH Token: </p> <pre><code>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); } }); </code></pre> <p>Based on the token this is how I build the Drive object</p> <pre><code>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(); } </code></pre> <p>I am able to upload the file using the following code:</p> <pre><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(); } </code></pre> <p>While trying to download the same file uploaded by this app, I get a 401 unauthorized error at this line <code>HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute()</code> from the following code snippet</p> <pre><code>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--&gt;"+title+" FieldID--&gt;"+fieldId+" DownloadURL--&gt;"+file.getDownloadUrl()); if (file.getDownloadUrl() != null &amp;&amp; file.getDownloadUrl().length() &gt; 0 ) { GenericUrl url = new GenericUrl(file.getDownloadUrl()); HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute(); InputStream isd = resp.getContent(); Log.e("MS", "MSV:: FileOutPutStream---&gt;"+getFilesDir().getAbsolutePath()+"/downloaded.txt"); } else { Log.e("MS", "MSV:: downloadURL for this file is null"); } } request.setPageToken(files.getNextPageToken()); } while (request.getPageToken()!=null &amp;&amp; request.getPageToken().length()&gt;0); } </code></pre> <p>Can anyone help me out and let me know what I am doing wrong???</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload