我正在编写一个 App Engine Web 应用程序,它从 Google Drive 上的文件夹中加载图片库。该代码在本地运行时运行良好(图像被检索并正确显示在网页上),但在 Google App Engine 上部署时它会中断。具体来说,启动如下方法时,返回的Drive服务为null,不抛出异常。凭据(SERVICE_ACCOUNT_EMAIL 和 PKCS12 文件)应该是正确的,因为它们在本地运行代码时有效。我在这里想念什么?提前致谢。
public static Drive getDriveService() {
HttpTransport httpTransport = new NetHttpTransport();
GsonFactory jsonFactory = new GsonFactory();
GoogleCredential credential;
Drive service = null;
List<String> scopes = Arrays.asList(DriveScopes.DRIVE);
try {
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
service = new Drive.Builder(httpTransport, jsonFactory, null).setApplicationName("MyAppName")
.setHttpRequestInitializer(credential).build();
} catch (Exception e) {
e.printStackTrace();
}
return service;
}