【发布时间】:2019-06-18 21:59:52
【问题描述】:
我的应用程序的输入是一个 Google Sheet URL,它可以获取凭据并读取 sheet 的内容。 当我的应用程序使用 Oauth2.0 从用户那里获得授权时,我使用下面 Google 文档中的示例代码。对于每个用户,我想存储一个唯一的凭据,这样当一个用户多次请求我的应用程序时,我就不必每次都创建和存储新的凭据(实际上这部分是在 AuthorizationCodeInstalledApp(flow, receiver ).authorize("user"),当“user”没有凭证时,它会创建一个新的凭证)。 在示例代码中,我认为它没有考虑多个用户向我的应用程序发送请求的情况,“用户”不能是多个用户的用户ID,所以“用户”必须针对每个用户进行调整,这意味着它不能一成不变。 因为凭据存储在本地,如果同一用户在短时间内多次请求,用户不必被重定向到谷歌登录页面来授予我的应用程序的授权。 我的问题是,当多个用户调用我的端点时,我能做些什么来给每个不同的用户一个唯一的用户 ID,每个用户都可以在短时间内输入多个 url。
我有一个想法,如果 Google API 有一种方法,它可以在获取凭据之前获取用户的唯一信息,例如 userID 的用户名。
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = KirbyGsheet.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(9998).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
【问题讨论】:
标签: google-sheets google-api google-oauth