问题
I am trying to list the files in my Google Drive account using a Java application. I followed the Java QuickStart tutorial in the Google developers page, and i have my client secret file and here is my code:
Drive service = DriveQuickstart.getDriveService();
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.size() == 0) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
However, when i run my Java application, I get the following error, which is a 401 Unauthorized.
[14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.luledu.eduMod.AddAssignment$GuiWindow.actionPerformed(AddAssignment.java:241) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.mouseClicked(GuiScreen.java:252) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.inventory.GuiContainer.mouseClicked(GuiContainer.java:350) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at com.luledu.eduMod.AddAssignment$GuiWindow.mouseClicked(AddAssignment.java:171) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.handleMouseInput(GuiScreen.java:344) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.gui.GuiScreen.handleInput(GuiScreen.java:313) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.runTick(Minecraft.java:1731) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.Minecraft.run(Minecraft.java:962) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.client.main.Main.main(Main.java:164) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at java.lang.reflect.Method.invoke(Method.java:606) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [14:12:01] [Client thread/INFO] [STDERR]: [java.lang.Throwable$WrappedPrintStream:println:748]: at GradleStart.main(Unknown Source) ` I would really appreciate it if someone can help me with this. Best,
来源:https://stackoverflow.com/questions/36476089/i-am-getting-an-error-in-listing-the-google-drive-files-using-java-401-unauthor