Google 个工作表请求 sheet 个用户访问 public sheet
Google Sheets requesting access to a users sheets for public sheet
我正在开发一个 Java 应用程序,它将连接 google sheet public 并从中读取数据。
当应用程序首次 运行 时,它会询问用户是否允许访问他们的 google sheet。该应用程序不会从用户自己的 sheet 中读取任何内容,只会读取 public 中的任何内容,因此不确定如何停止询问。
我正在使用
请求数据
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = GoogleAPI.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
和
public ArrayList<Template> loadTemplates() throws IOException, GeneralSecurityException {
ArrayList<Template> templateList = new ArrayList();
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final String spreadsheetId = "****";
final String range = "****";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
System.out.println("No data found.");
} else {
for (List row : values) {
/ /Code here
}
}
return templateList;
}
将其切换为使用 API 而不是我只读过的 oAuth。
在方法中添加了以下内容。
HttpRequestInitializer httpRequestInitializer = request -> {
request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
};
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
对于可能有相同问题的任何人。
这是仅使用 Google Cloud Platform API 密钥从 public Google Sheet 读取数据的方法。
public static void main(String[] args) throws IOException, GeneralSecurityException {
List<List<Object>> result = loadData();
for (List row : result) {
System.out.println(row);
}
}
public static List<List<Object>> loadData() throws IOException, GeneralSecurityException {
JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
String APPLICATION_NAME = ".";
//Insert here your API key created by Google Cloud Platform
String API_KEY = "";
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
//Insert here Google Sheet ID
final String spreadsheetId = "";
//D to read all data in sheet
final String range = "A2:D";
HttpRequestInitializer httpRequestInitializer = request -> {
request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
};
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
System.out.println("No data found.");
}
return values;
}
我正在开发一个 Java 应用程序,它将连接 google sheet public 并从中读取数据。 当应用程序首次 运行 时,它会询问用户是否允许访问他们的 google sheet。该应用程序不会从用户自己的 sheet 中读取任何内容,只会读取 public 中的任何内容,因此不确定如何停止询问。
我正在使用
请求数据private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = GoogleAPI.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
和
public ArrayList<Template> loadTemplates() throws IOException, GeneralSecurityException {
ArrayList<Template> templateList = new ArrayList();
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final String spreadsheetId = "****";
final String range = "****";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
System.out.println("No data found.");
} else {
for (List row : values) {
/ /Code here
}
}
return templateList;
}
将其切换为使用 API 而不是我只读过的 oAuth。
在方法中添加了以下内容。
HttpRequestInitializer httpRequestInitializer = request -> {
request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
};
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
对于可能有相同问题的任何人。 这是仅使用 Google Cloud Platform API 密钥从 public Google Sheet 读取数据的方法。
public static void main(String[] args) throws IOException, GeneralSecurityException {
List<List<Object>> result = loadData();
for (List row : result) {
System.out.println(row);
}
}
public static List<List<Object>> loadData() throws IOException, GeneralSecurityException {
JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
String APPLICATION_NAME = ".";
//Insert here your API key created by Google Cloud Platform
String API_KEY = "";
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
//Insert here Google Sheet ID
final String spreadsheetId = "";
//D to read all data in sheet
final String range = "A2:D";
HttpRequestInitializer httpRequestInitializer = request -> {
request.setInterceptor(intercepted -> intercepted.getUrl().set("key", API_KEY));
};
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, httpRequestInitializer)
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
System.out.println("No data found.");
}
return values;
}