Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

java - Read time out issue with Google Drive

I am using Drive SDK v3 to upload/download from drive. The authorization is web page based (OAuth2) and not API key where user will authenticate their google account and grant required permission. Recently I am facing Read Time Out issue when exporting a document from drive. Here is the authorization flow.

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

I found the following code to manually set request time out from google-http-client documentation.

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

When I use this, I get the following error.

{
  "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."
}

When I checked the API Console, Drive API is not enabled. But if I enable it then I need to use API based authentication.

But I need to keep user based authorization but needs to solve the timeout issue by extending the timeout. Please help

question from:https://stackoverflow.com/questions/66046494/read-time-out-issue-with-google-drive

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are going to be using this for a single user you should consider using a service account

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = GoogleCredential
        .fromStream(new FileInputStream(KEY_FILE_LOCATION))
        .createScoped(DriveScopes.all());

    // Construct the Analytics Reporting service object.
    return new Drive.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...