我正在使用进行身份验证域范围的委派 https://developers.google.com/drive/web/delegation.
我正在恢复谷歌驱动器服务,如下所示:com.google.api.services.drive.Drive@6ebd27b9
这是我尝试检索的文件的链接:https://docs.google.com/a/rmi.org/document/d/1JRS8SLzaAD2U4cG-GbDMbVN25Dp6f_uUYyl6ERMPAno/edit
我将此值作为文件 ID 传递:1JRS8SLzaAD2U4cG-GbDMbVN25Dp6f_uUYyl6ERMPAno
.
当代码到达这一行时:File file = service.files().get(fileId).execute();
我收到此错误:
An error occured: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 OK
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "File not found: 1JRS8SLzaAD2U4cG-GbDMbVN25Dp6f_uUYyl6ERMPAno",
"reason" : "notFound"
} ],
当我尝试使用底部的工具查找有问题的文件时这一页 https://developers.google.com/drive/v2/reference/files/get,如果我打开 Oauth 2.0,我会收到 200 响应代码以及有关相关文件的信息。
我在这里看过很多类似的问题,包括this https://stackoverflow.com/questions/21188752/get-childrens-files-from-a-an-id-folder-in-google-drive问题,但我看不出我设置权限的方式有什么问题:
- 对于我的项目,Drive API 和 Drive SDK 均位于 API 和 Auth 下。
- 在“凭据”下,我可以看到我的客户 ID、电子邮件地址和公钥指纹
- 我还在我的域的管理控制台上验证了在“安全”>“API 参考”下,选中了“启用 API 访问”。
- 安全 > 高级设置 > 管理 API 客户端访问添加了以下范围:https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive与服务帐户的客户端 ID。
- Security > Advanced > Manage Oauth Domain key:
- 选中“启用此消费者密钥”复选框。
- 有一个 oAuth 消费者密码。
- 没有 X.509 证书。
- 未选中“两条腿 OAuth 访问控制允许访问所有 API”。
这是我获得驱动服务的地方:
public static Drive getDriveService() throws GeneralSecurityException,
IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
//.setServiceAccountScopes(DriveScopes.DRIVE)
.setServiceAccountScopes(SCOPE)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential).build();
return service;
我将这些变量定义如下:
/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "[email protected] /cdn-cgi/l/email-protection";
/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/actualpathhere/HowToListing/war/resources/actualpd12filename.p12";
private static final List<String> SCOPE = Arrays.asList("https://www.googleapis.com/auth/drive.readonly");
这是我尝试获取文件元数据的地方:
private static List<File> retrieveAllFiles(Drive service) throws IOException {
List<File> result = null;
try {
String fileId = "1JRS8SLzaAD2U4cG-GbDMbVN25Dp6f_uUYyl6ERMPAno";
File file = service.files().get(fileId).execute();
System.out.println("Title: " + file.getTitle());
System.out.println("Description: " + file.getDescription());
System.out.println("MIME type: " + file.getMimeType());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
return result;
}
谁能给我任何关于可能发生的事情的提示?