Mohammed shebin
Mohammed shebin

Reputation: 479

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

Upvotes: 1

Views: 556

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

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

Upvotes: 1

Related Questions