Unable to find newly created spreadsheet from java on google drive

Viewed 77

I'm trying to create a spreadsheet using the google service account`s credentials from java, at end of execution I got a newly created spreadsheet Id but when I opened my google drive I haven't got any sheet with the newly created id. please help me to solve this issue.

this is the method I used to create a new sheet :

public static void createSheet() throws IOException {
        String title =  LocalDateTime.now().toString() ;
        Spreadsheet spreadsheet = new Spreadsheet()
                .setProperties(new SpreadsheetProperties()
                        .setTitle(title));
        spreadsheet = service.spreadsheets().create(spreadsheet)
                .setFields("spreadsheetId")
                .execute();
        spreadsheetId = spreadsheet.getSpreadsheetId();
        System.out.println("Spreadsheet ID: " + spreadsheet.getSpreadsheetId());
    }

and I had also tried to access the spreadsheet id through the browser but it asked me for access permission.

1 Answers

A service account is not you. A service account is a dummy user with its own google drive account.

If you do a file.list you will find that the new sheet was created on the Service accounts Google drive account.

If you would like to create it on your own account then create a directory on your personal drive account, share the directory with the service account like you would another user simple take the service account email address and share it that way.

Then when you create your sheet you will need to set the parents filed to that id of the directory you would like it to be uploaded to you can use the setParents method to add it to the file metadata.

.setProperties(new SpreadsheetProperties()
.setParents(fileid)
.setTitle(title));

If you would like to understand more about how service accounts work you may find this video interesting. It also shows exactly how to share a folder with the service account. Should you be using a Google service account after in 2021?

Related