Google 传播sheet 创建一个新的 sheet 并允许所有用户拥有 link

Google spreadsheet create a new sheet with permission for all users have link

我尝试使用 google sheet API v4 创建一个新的 sheet。我创建成功了。但是,我发现 sheet 正在设置,因为只有我一个人可以访问。我的目标是让拥有 url link 的用户可以访问 sheet in Java 的请求。

我试图在创建 spreadsheet 时将 google 凭证设置为 null,但它用于在驱动器中创建 spreadsheet。因此,这是行不通的。 我浏览了它的库 class 和 api,但我找不到与权限相关的任何内容。

我不太确定我创建传播的方式sheet 是否正确。这是我的代码:

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();
    }
}

正如@tehhowch 所说,权限应该通过驱动器处理 API。这里只是更新一个答案。

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();
}

参考:Google api permission