如何将凭据硬编码到我的 Google Drive 服务,以便应用程序的用户始终无需身份验证即可访问我的文件?
我找到了使用 Java SKD 的解决方案,但这些库不能很好地与 Android 配合使用:https://developers.google.com/drive/service-accounts#use_service_accounts_as_application-owned_accounts https://developers.google.com/drive/service-accounts#use_service_accounts_as_application-owned_accounts
有类似任务成功尝试的例子吗?
好的,我已经找到了解决我的问题的方法。
当然,我的应用程序是Android应用程序,我不想让用户登录/使用任何凭据连接到我的云端硬盘,最后我可以使用默认的云端硬盘网络应用程序操作文件。
- 需要采取 Seps:创建服务帐户 as in this https://developers.google.com/drive/service-accounts#google_apis_console_project_service_accounts例子。
- 下载私钥API访问站点并把它例如。在
资产文件夹。
- Download and import these libraries:
- google-api-client-1.13.2-beta.jar
- google-api-client-android-1.13.2-beta.jar
- google-api-services-drive-v2-rev60-1.13.2-beta.jar
- 谷歌-http-client-1.13.1-beta.jar
- 谷歌-http-client-android-1.13.1-beta.jar
- 谷歌-http-client-gson-1.13.1-beta.jar
- 谷歌-http-client-jackson2-1.13.1-beta.jar
- 谷歌-oauth-client-1.13.1-beta.jar
- gson-2.1.jar
- 番石榴-jdk5-13.0.jar
- 杰克逊核心-2.0.5.jar
- jsr305-1.3.9.jar
-
实施 Drive 服务 getter:
public Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(G_SERVICE_EMAIL)
.setServiceAccountScopes(DriveScopes.DRIVE)
.setServiceAccountPrivateKeyFromP12File(PKC_12_FILE)
.build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential)
.build();
return service;
}
Where G_SERVICE_EMAIL电子邮件地址来自API访问站点 and PKC_12_文件是之前下载的私钥。
-
允许您的服务访问您的云端硬盘中的文件夹:在文件夹的共享选项中驱动应用程序 https://drive.google.com允许用户使用电子邮件:G_SERVICE_EMAIL读/写访问。
PKC 文件集成
private File getTempPkc12File() throws IOException {
InputStream pkc12Stream = getAssets().open("this-is-your-unique-hash-privatekey.p12");
File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
OutputStream tempFileStream = new FileOutputStream(tempPkc12File);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = pkc12Stream.read(bytes)) != -1) {
tempFileStream.write(bytes, 0, read);
}
return tempPkc12File;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)