Ho automatizzato con successo il processo per spostare i dati da Google Big Query, a Google Storage. Ora ho bisogno di scaricare i dati da Google Storage nel mio ambiente in modo automatico.Scarica file da Google Storage utilizzando Java
Sto tentando di eseguire una normale richiesta HTTP, ma autorizzo prima. Quindi la mia richiesta HTTP è
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(authorize());
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
E il mio codice di autorizzazione è
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(BigQueryConsumer.class.getResourceAsStream("/secret.json")));
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
SCOPES).setDataStoreFactory(fileDataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
Dove le seguenti costanti sono
- CREDENTIALS_DIRECTORY: ".oauth-credenziali"
- JSON_FACTORY: JacksonFactory. getDefaultInstance()
- SCOPES: un elenco di stringhe che hanno appena "https://www.googleapis.com/auth/devstorage.full_control"
- HTTP_TRANSPORT: nuova NetHttpTransport()
Che cosa mi manca durante il processo di autenticazione/autorizzazione? Sto ottenendo
Exception in thread "main" com.google.api.client.http.HttpResponseException: 401 Unauthorized
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Si potrebbe anche voler provare [ 'gcloud-java'] (http://googlecloudplatform.github.io/gcloud-java/), ecco qualche [codice di esempio] (https: //github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java). –