I'm using the Google Sheets API to obtain sheet data for a Java project. All works as expected locally, but I'm using the verbose permissions scope https://www.googleapis.com/auth/spreadsheets which "Allows read/write access to the user's sheets and their properties.". I would prefer to not provide this app access to all of the spreadsheets in my Google Drive (have only done so locally temporarily).
Ideally, I'd like to request permission for read/write access to a file using a file's ID. Is this possible?
If not possible, I'm guessing that the https://www.googleapis.com/auth/drive.file scope which provides "Per-file access to files created or opened by the app." is the closest I can get. I haven't managed to find a way to open the file with this app. How would I go about doing that?
Or if both of the above solutions aren't ideal or possible, let me know what you'd recommend.
Thank you!
I know this was posted quite a while ago, but I'll give my answer to help out future developers coming across this in the future.
I think using service accounts would give you the functionality you are looking for here. Service accounts are kind of like "bot" users that users can share documents with, and then your server can login to this service account to access those documents. Instead of having to request access to a user's entire google drive or google sheets, you can have them share the documents with you manually, which I think would be more comfortable for most users.
Here is an example of how to set this up in Node.js, but the ideas should translate fairly readily to Java.
Scopes grant you access across an api there is no way to limit it to a single file or group of files.
Google Sheets API, v4 Scopes
There is no way to limit permissions to a single file. Assuming that the file you are editing was created by your application then
https://www.googleapis.com/auth/drive.file
should be a valid optionSample
Java quickstart
Update 2020
There is a way to grant per file access.
I think, this is what you're requesting for. ( https://developers.google.com/sheets/api/quickstart/java , https://www.youtube.com/watch?v=zDxTSUWaZs4 ) I am using this code to access a google sheet via ID
You might need to modify this code snippet according to your needs a bit. I used this as a part of an app of mine.
So what will this code do? On execution, this code will connect to google with your personal API key, which I named credentials.json. (Create your own at: https://developers.google.com/+/web/api/rest/oauth ) After successfull authentication, you will be able to access a google sheet with a particular ID.