Ich habe den Prozess zum Verschieben von Daten von Google Big Query auf Google Storage erfolgreich automatisiert. Jetzt muss ich die Daten von Google Storage auch automatisiert in meine Umgebung herunterladen.Herunterladen von Dateien aus Google Storage mit Java
Ich versuche, eine normale HTTP-Anfrage zu tun, aber vorher autorisieren. Also meine HTTP-Anforderung ist
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(authorize());
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
Und mein Autorisierungscode ist
/** 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");
}
Wo die folgenden Konstanten
- CREDENTIALS_DIRECTORY sind: ".oauth-credentials"
- JSON_FACTORY: JacksonFactory. getDefaultInstance()
- SCOPES: Eine Liste der Zeichenfolge mit nur "https://www.googleapis.com/auth/devstorage.full_control"
- HTTP_TRANSPORT: neue NetHttpTransport()
Was soll ich während der Authentifizierung/Autorisierung Prozess fehlt? Ich bekomme
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>
Sie möchten vielleicht auch ausprobieren [ 'gcloud-java'] (http://googlecloudplatform.github.io/gcloud-java/), hier einige [Beispielcode] (https: //github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java). –