How to use Google Drive SDK in Android project with hardcoded credentials

后端 未结 2 2143
甜味超标
甜味超标 2020-12-01 23:17

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

I have found solution using Java SKD bu

相关标签:
2条回答
  • 2020-12-01 23:46

    Ok, I've found solution for my problem.

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

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

    5. 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.

      PKC FILE INTEGRATION

      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;
      }
      
    0 讨论(0)
  • 2020-12-01 23:48

    You probably don't want to do this. If you have an access token you can always add it as a URL parameter access_token=MY_ACCESS_TOKEN to the request URL.

    0 讨论(0)
提交回复
热议问题