Google Drive API Check if File is Shared With Me - java

I am creating an android app which uses Google Sheets API V4 and Google Drive API V2 to edit a Google Spreadsheet. But I am having an issue. I need to be able to check if the spreadsheet is shared with the user who is logged into the device and if they can edit it or if they are an owner. I have tried
Boolean canEdit = driveService.files().get(fileId).execute().getEditable();
and
Boolean canEdit = driveService.files()
.get(fileId).execute().getCapabilities().getCanEdit();
but both of those seem to only work if the user owns the file. I think that that is because the command files().get(id) can only get the files in the users drive; not any files that are shared with the user. I have also tried
String role = driveService.permissions().get(fileId, driveService
.about().get().execute().getPermissionId()).execute().getRole();
but this seems to also only look for files in the users Drive. All of these commands throw 404 Not Found if the user is not an owner.
Please help me!

As discussed in Handling API Errors, 404: File not found could be that the user does not have read access to a file.
And, AFAIK, requests which needs access to user data requires s particular scope of access. However, as noted in Authorizing Android Apps, the Google Drive Android API currently only supports drive.file and drive.appfolder authorization scopes.
With this, you may want to authorize your requests using the Google APIs Java Client and instead use the Google Drive web service to search for files that are shared with you.

Related

The proper way to manage a Google Drive account using Java console application?

I have enabled Google Drive API on Google Developer Console and created a service account credential which is bundled with a service account ID and key ID. Despite that I have tried various ways to manage my own Google Drive account, I could not find a solution which fits for my aim. I need to upload, download and delete the files stored in my own Google Drive. The application that I am going to connect Google Drive is a console one which will be solely used for an academic research.
it's pretty simple. The steps to achieve this are at How do I authorise an app (web or installed) without user intervention? (canonical ?)
Be careful about how securely the refresh token is being stored and what scopes you grant to the app.

How to Hide/ByPass Authoriaztion Pop Up while accessing Google Drive API

I am using Google Drive API's using JAVA for getting drive files, i want to create a background job which will fetch the google drive files
without any user interaction (eg asking for user to explicity Authorize).
In the example given below by google, after running this java program we need to explicitly hit URL in browser & click on Authorize
for full execution of the program.
https://developers.google.com/drive/v3/web/quickstart/java
Using the Service Account I am able to fetch the google drive contents without any kind of user action (eg click on allow to authorize).
I want to acheive the same flow for individual user accounts, I have the list of emailid and passowords for google accounts.
Other than service accounts, is there any method so that i connect to the authorization url in background using HTTPPost request and
fetch the file list.
I don't think there's another way of doing this aside from Service Accounts which you are currently using. You can try to use the Google Playground but this is for testing purposes only and not for production.

Store files in google drive from Google App Engine

All,
I have a google application engine (java) that requires to store some images. I tried using Blobs and storing them in datastore but as you know there is a size limit on data that can be stored in datastore.
So as result I'm storing the images on a different server and store the path in my datastore and all works fine.
Now I'm thinking on using a google drive folder instead of using a server to upload the files to the drive and using the share link to display them later.
I've seen https://developers.google.com/drive/web/quickstart/java and got it to work fine. When I try to use it in my application however obviously this won't work as the code is assuming a credential for a local user.
I created a service key on my application and want to change the sample code above to use it but I'm not sure if that's the correct approach.
Tried searching for samples but can't find anyone that takes the same approach. Is there a working sample that shows how to authenticate an application not a user and let's say store a file in google drive?
I've also seen https://developers.google.com/drive/web/examples/ please note what I want is to store files in my google drive and not the user's google drive. So if user A and user B come to my app, they shouldn't have to authorize my application and should both be able to upload a file to my google drive.
I don't know if this can be done directly from their browser or I have to move the file to my application (appspot) and then push it to google drive.
Thanks

Permanently authorize a Google Sheets

I'm making a project that involves an App Engine (Java) Server that creates a spreadsheet on a weekly basis. The spreadsheet will be created on behalf of an actual Google user.
How do I configure my app to be given permanent[1] access to read/write/create a Google Sheets?
Can I use the same method[1] to access my user's Google Sheets permanently?
[1]The app is currently using a permanent (until revoked) access to the Admin API. I need a similar kind of OAuth2. https://developers.google.com/admin-sdk/directory/v1/guides/delegation.

Creating files go to which account using Google Drive APIs when authenticating with a service account?

I'm trying to make use of the Google Drive APIs to send a log file created by our application to our company's Google Drive Account that we share for Android Development. Most of the examples showed how to use oAuth2 to authenticate with the end-user's Google Drive Account, but we want the files to be sent to just our Google Drive Account.
After some searching I found that I should create an API project for our app and use a Service account associated with that API Project to generate a key which we include in our project as a raw resource to authenticate using oAuth2 to get a GoogleCredential which is needed to build the Drive service we will use to make our API calls.
After authenticating properly, I was able to insert a file using the code example on the Google Drive API site and that appears to work properly, however I cannot see the file in our Google Drive account which created the API Project/Service account. Despite this, if I call Drive.files().list().execute() and output the FileList to a string I was able to see the files our app has previously inserted into the Service account's Google Drive.
So my question is: Is there a new Google Drive account created for the Service account that is separate from the Google Drive account that created the Service account? If so, is there a way to login to that account and view the files using the normal Google Drive Web UI so that we can get to the log files without writing some other application that authenticates with the Service account and then grabs the files?
It appears my question ended up being a duplicate, the other question one was just a little bit hard to find for me. The question is I can't see the files and folders created via code in my Google Drive and it was answered by a Google employee: https://stackoverflow.com/a/12218662/994519

Categories