我想上傳文件到谷歌驅動器使用驅動器休息API,繼此tutorial。權限異常,同時插入文件在谷歌驅動器休息API Java
對於授權,功能是:
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
DriveQuickstart.class.getResourceAsStream("client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
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());
return credential;
}
我爲獲得所有文件的功能存在於驅動器工作完全正常。
private static void listFiles(Drive service) throws IOException
{
// Print the names and IDs for up to 10 files.
FileList result = service.files().list().setMaxResults(10).execute();
List<File> files = result.getItems();
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.getTitle(), file.getId());
}
}
}
但是當我嘗試創建和插入驅動器的文件,我得到異常,功能異常如下:
private static void insertFile(Drive service, String title, String description) {
// File's metadata.
File file = new File();
file.setTitle(title);
file.setDescription(description);
file.setMimeType("application/vnd.google-apps.drive-sdk");
try {
file = service.files().insert(file).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Print the new file ID.
System.out.println("File ID: %s" + file.getId());
}
的例外是:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
文件ID:%在com.google.api.cli.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 處com.google.api.client.googleapis.services處縮小爲 。 json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 位於com.google.api.cli.cli.client.googleapis上的com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 。 services.AbstractGoogleClientRequest $ 1.interceptResponse(AbstractGoogleClientRequest.java:321) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest .executeUnparsed(AbstractGoogleClientRequest.java:419) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute (AbstractGoogleClientR equest.java:469) at com.teamchat.google.sheet.integration.DriveQuickstart.insertFile(DriveQuickstart.java:137) at com.teamchat.google.sheet.integration.DriveQuickstart.main(DriveQuickstart.java:110)
這只是一個簡單的java項目並使用OAuth。
所以我能夠得到文件的列表,但無法在谷歌驅動器中插入文件。 有人可以告訴我我做錯了什麼。
謝謝您的回答,所以我應該設置範圍的地方,如果是又在哪裏?我讀了很多關於選擇範圍的信息,但無法找到設置或使用它的任何地方。 –
你應該已經設置了一個範圍列表,看看你的SCOPES屬性。我認爲你設置只讀訪問像'DRIVE_METADATA_READONLY'或'DRIVE_READONLY' – Kalem
感謝您的幫助。 –