【发布时间】:2021-05-08 19:18:47
【问题描述】:
我正在使用 Drive SDK v3 从驱动器上传/下载。授权是基于网页的 (OAuth2) 而不是 API 密钥,用户将在其中验证他们的谷歌帐户并授予所需的权限。最近,我从驱动器导出文档时遇到了读取超时问题。这是授权流程。
InputStream in = DriveUtils.class
.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to "
+ DATA_STORE_DIR.getAbsolutePath());
Drive driveService = Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
我从 google-http-client 文档中找到了以下代码来手动设置请求超时。
Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.setHttpRequestInitializer(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setReadTimeout(5 * 60000); // 5 minutes
}
})
.build();
当我使用它时,我收到以下错误。
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"reason" : "dailyLimitExceededUnreg",
"extendedHelp" : "https://code.google.com/apis/console"
} ],
"message" : "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
当我检查 API 控制台时,Drive API 未启用。但是如果我启用它,那么我需要使用基于 API 的身份验证。
但我需要保持基于用户的授权,但需要通过延长超时来解决超时问题。请帮忙
【问题讨论】:
-
授权码有问题,如果它没有弹出并请求用户访问。
-
如果我不设置 httpRequestInitializer,@DalmTo 授权代码工作正常。但有时会出现 Read Time Out。
-
已超过未经身份验证使用的每日限制 github.com/googlearchive/drive-android-quickstart/blob/master/…
-
@DaImTo Auth 在我设置 httpRequestInitializer 时不起作用,否则它就可以正常工作。感谢您的链接,但我正在使用 java 在后端执行此操作。
-
如果您在后端执行此操作,您打算如何允许用户对您的应用程序进行身份验证?您应该使用 GoogleAuthorizationCodeFlow.Builder
标签: java google-api google-drive-api google-oauth google-api-java-client