0

I tried to user the google sheet API v4 to create a new sheet. I create it successfully. However, I found that the sheet is setting as I am the only one is able to access. My goal is to allow users have the url link can access the sheet in Java request.

I tried to set google credidential to null when create spreadsheet, but it use as to create the spreadsheet in drive. Thus, this is not work. I browser it library class and api, but I cannot found anything related to the permission.

I am not so sure the way I create the spreadsheet is on right way. Here is my code:

public class CreateSpreadSheetTask extends AsyncTask<Void, Void, Void> { private com.google.api.services.sheets.v4.Sheets mService = null; private Exception mLastError = null; private String title = null; private CreateSpreadsheetListener listener = null; public CreateSpreadSheetTask(GoogleAccountCredential credential, String title, CreateSpreadsheetListener listener) { HttpTransport transport = AndroidHttp.newCompatibleTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); mService = new Sheets.Builder( transport, jsonFactory, credential) .setApplicationName("Google Sheets API Android Quickstart") .build(); this.title = title; this.listener = listener; } @Override protected Void doInBackground(Void... params) { try { this.listener.onRecvCreateSpreadsheet(getSpreadsheetFromApi()); } catch (Exception e) { e.printStackTrace(); mLastError = e; cancel(true); } return null; } private Spreadsheet getSpreadsheetFromApi() throws IOException { Spreadsheet spreadsheet = new Spreadsheet(); SpreadsheetProperties properties = new SpreadsheetProperties(); properties.setTitle(this.title); spreadsheet.setProperties(properties); Sheets.Spreadsheets.Create request = mService.spreadsheets().create(spreadsheet); return request.execute(); } } 
1
  • 1
    Use the Drive REST API to share the document / make it public. The file id for Drive is the same as the spreadsheetId that the Sheets API gives you in the response from Create. Commented Jul 21, 2018 at 16:26

1 Answer 1

1

As @tehhowch said the permission should handle via the Drive API. Here is just update an answer for this.

private void updateProperty(com.google.api.services.drive.Drive driveService, String fileId, String email) throws IOException { BatchRequest batch = driveService.batch(); Permission clientPermission = new Permission(); clientPermission.setEmailAddress(email); clientPermission.setRole("writer"); clientPermission.setType("user"); driveService.permissions().create(fileId, clientPermission) .setFields("id") .setSendNotificationEmail(true) .queue(batch, new JsonBatchCallback() { @Override public void onSuccess(Object o, HttpHeaders responseHeaders) throws IOException { Log.e("permissione success", String.valueOf(o)); listener.onRecvShareSpreadsheet(true); } @Override public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException { Log.e("permissione error", e.getMessage()); listener.onRecvShareSpreadsheet(false); } }); batch.execute(); } 

ref: Google api permission

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.