如何使用谷歌驱动器SDK中带硬codeD凭据Android项目 [英] How to use Google Drive SDK in Android project with hardcoded credentials

查看:115
本文介绍了如何使用谷歌驱动器SDK中带硬codeD凭据Android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能硬code凭证到我的谷歌驱动器服务使应用程序的用户将获得存取权限到我的文件无需身份验证?

How can I hardcode credentials to my Google Drive Service so users of the app will always get acces to my files without auth?

我发现使用Java SKD但这些库不与Android合作以及解决办法:<一href="https://developers.google.com/drive/service-accounts#use_service_accounts_as_application-owned_accounts" rel="nofollow">https://developers.google.com/drive/service-accounts#use_service_accounts_as_application-owned_accounts

I have found solution using Java SKD but these libraries doesn't work well with Android: https://developers.google.com/drive/service-accounts#use_service_accounts_as_application-owned_accounts

是类似任务成功的尝试有什么例子?

Are there any examples of successful attempts of similar tasks?

推荐答案

我的问题好了,我已经找到解决方案。

Ok, I've found solution for my problem.

当然,我的应用程序是一个Android的,我dont'n希望让用户登录/使用任何凭据连接到我的驱动器,最后我可以使用默认的驱动器的Web应用程序文件进行操作。

Of course my app is an Android one, I dont'n want make user to log in/use any credentials to connect to my Drive, and lastly I can manipulate files using default Drive web app.

  1. SEPS需要采取:创建的服务帐户的如<一个href="https://developers.google.com/drive/service-accounts#google_apis_console_project_service_accounts"相对=nofollow>这个 例。
  2. 在下载私钥的 API访问的网站的,并把它放在如。在 资产文件夹。
  3. 下载并导入这些库:
    • 在谷歌的API客户端-1.13.2-beta.jar
    • 在谷歌的API客户端,Android为1.13.2-beta.jar
    • 谷歌API的服务驱动-V2-rev60-1.13.2-beta.jar
    • 在谷歌的HTTP客户端-1.13.1-beta.jar
    • 在谷歌的HTTP客户端,Android为1.13.1-beta.jar
    • 在谷歌的HTTP客户端GSON-1.13.1-beta.jar
    • 在谷歌的HTTP客户端jackson2-1.13.1-beta.jar
    • 在谷歌的OAuth客户端-1.13.1-beta.jar
    • GSON-2.1.jar
    • 番石榴jdk5-13.0.jar
    • 杰克逊核心-2.0.5.jar
    • jsr305-1.3.9.jar
  1. Seps need to be taken: Create service account as in this example.
  2. Download private key API Access site and put it eg. in assets folder.
  3. 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
    • google-http-client-1.13.1-beta.jar
    • google-http-client-android-1.13.1-beta.jar
    • google-http-client-gson-1.13.1-beta.jar
    • google-http-client-jackson2-1.13.1-beta.jar
    • google-oauth-client-1.13.1-beta.jar
    • gson-2.1.jar
    • guava-jdk5-13.0.jar
    • jackson-core-2.0.5.jar
    • jsr305-1.3.9.jar

实现驱动服务的getter:

Implement Drive service 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;
}

其中 G_SERVICE_EMAIL API访问的网站 PKC_12_FILE 是previously下载私钥的电子邮件地址。

Where G_SERVICE_EMAIL is email address from API Access site and PKC_12_FILE is previously downloaded private key.

允许您服务,从您的驱动器访问文件夹:在文件夹的共享选项中驱动器应用允许用户与电子邮件: G_SERVICE_EMAIL 读/写访问

Allow your service to access folder from your Drive: in sharing options of the folder in Drive app allow user with email: G_SERVICE_EMAIL read/write access.

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;
}

这篇关于如何使用谷歌驱动器SDK中带硬codeD凭据Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆