Guten Tag,
Ich habe mir die API von Google Drive angeschaut. Dafür habe ich auf meinen Windows 7 Rechner folgende Bibliotheken in mein Eclipseprojekt importiert:
Die Json Datei habe ich in einem Package in meinem Projekt integriert, welche ich mir von Google Drive geholt habe. Mein Problem ist jetzt noch, dass ich eine Fehlermeldung bekomme und ich weiß nicht warum.
Ich denke, dass es an der Methode des Objektes clientSecret.getDetails() liegt. Aber ich kann mir das nicht erklären.
Vielen Dank!
Ich habe mir die API von Google Drive angeschaut. Dafür habe ich auf meinen Windows 7 Rechner folgende Bibliotheken in mein Eclipseprojekt importiert:
- commons-logging-1.1.1.jar
- google-api-client-1.22.0.jar
- google-api-services-drive-v2-rev230-1.22.0.jar
- google-http-client-1.22.0.jar
- google-http-client-jackson2-1.22.0.jar
- google-oauth-client-1.22.0.jar
- google-oauth-client-java6-1.22.0.jar
- google-oauth-client-jetty-1.22.0.jar
- gson-2.1.jar
- httpclient-4.0.1.jar
- httpcore-4.0.1.jar
- jackson-core-2.1.3.jar
- jsr305-1.3.9.jar
Java:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Collections;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Preconditions;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
//https://github.com/google/google-api-java-client-samples/tree/master/drive-cmdline-sample/src/main/java/com/google/api/services/samples/drive/cmdline
class Main {
private static final String APPLICATION_NAME = "author-Schule/1.0";
private static final String UPLOAD_FILE_PATH = "I:/Upload.txt";
private static final String DIR_FOR_DOWNLOADS = "I:/Download";
private static final java.io.File UPLOAD_FILE = new java.io.File(UPLOAD_FILE_PATH);
/** Directory to store user credentials. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/drive_sample");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static Drive drive;
public static void main(String[] args) {
new Main();
}
private static Credential authorize() throws IOException {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(Main.class.getClassLoader().getResourceAsStream("path to package/client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println( "Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
+ "into drive-cmdline-sample/src/main/resources/client_secrets.json");
System.exit(1);
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(DriveScopes.DRIVE_FILE)).setDataStoreFactory(dataStoreFactory).build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
public Main() {
Preconditions.checkArgument(!UPLOAD_FILE_PATH.startsWith("Enter ") && !DIR_FOR_DOWNLOADS.startsWith("Enter "),
"Please enter the upload file path and download directory in %s", Main.class);
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// authorization
Credential credential = authorize();
// set up the global Drive instance
drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
// run commands
View.header1("Starting Resumable Media Upload");
File uploadedFile = uploadFile(false);
View.header1("Updating Uploaded File Name");
File updatedFile = updateFileWithTestSuffix(uploadedFile.getId());
View.header1("Starting Resumable Media Download");
downloadFile(false, updatedFile);
View.header1("Starting Simple Media Upload");
uploadedFile = uploadFile(true);
View.header1("Starting Simple Media Download");
downloadFile(true, uploadedFile);
View.header1("Success!");
return;
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
/** Uploads a file using either resumable or direct media upload. */
private static File uploadFile(boolean useDirectUpload) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle(UPLOAD_FILE.getName());
FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);
Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());
return insert.execute();
}
/** Updates the name of the uploaded file to have a "drivetest-" prefix. */
private static File updateFileWithTestSuffix(String id) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle("drivetest-" + UPLOAD_FILE.getName());
Drive.Files.Update update = drive.files().update(id, fileMetadata);
return update.execute();
}
/** Downloads a file using either resumable or direct media download. */
private static void downloadFile(boolean useDirectDownload, File uploadedFile) throws IOException {
// create parent directory (if necessary)
java.io.File parentDir = new java.io.File(DIR_FOR_DOWNLOADS);
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
MediaHttpDownloader downloader = new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);
}
}
Java:
Aug 02, 2016 5:05:44 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNUNG: unable to change permissions for everybody: C:\Users\Falk\.store\drive_sample
Aug 02, 2016 5:05:44 PM com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
WARNUNG: unable to change permissions for owner: C:\Users\Falk\.store\drive_sample
java.lang.IllegalArgumentException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82)
at de.falk.drive.Main.authorize(Main.java:56)
at de.falk.drive.Main.<init>(Main.java:80)
at de.falk.drive.Main.main(Main.java:49)
Vielen Dank!