0

Google ドライブ SDK を使用してファイルを Google ドライブ ディスクにアップロードする必要があります。私はそれを行うための簡単なクラスを持っています:

public class DriveApiTest {
    private static String CLIENT_ID = "...";
    private static String CLIENT_SECRET = "...";

    // private static String REDIRECT_URI = "...";
    private static String REDIRECT_URI = "...";

    static HttpTransport httpTransport = new NetHttpTransport();
    static JsonFactory jsonFactory = new JacksonFactory();
    static String parentId = "0B8Myj-AtyvWAWlNTQmpMdWxCRTQ";

    public static void start() throws IOException {
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
                Arrays.asList(DriveScopes.DRIVE)).setAccessType("online")
                .setApprovalPrompt("auto").build();

        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI)
                .build();
        System.out
                .println("Please open the following URL in your browser then type the authorization code:");
        System.out.println("  " + url);
        try {
            Desktop.getDesktop().browse(new URI(url));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse response = flow.newTokenRequest(code)
                .setRedirectUri(REDIRECT_URI).execute();

        GoogleCredential credential = new GoogleCredential()
                .setFromTokenResponse(response);

        Drive service = new Drive.Builder(httpTransport, jsonFactory,
                credential).setApplicationName("Test").build();

        File body = new File();
        body.setTitle("jeszcze inny Plik w podfolderze");
        body.setMimeType("text/csv");
        body.setParents(Arrays.asList(new ParentReference().setId(parentId)));

        java.io.File fileContent = new java.io.File(
                "C:\\Users\\TomekZ\\Desktop\\RoboczeExcele\\Person.csv");
        FileContent mediaContent = new FileContent("text/csv", fileContent);

        File file = service.files().insert(body, mediaContent).execute();
        System.out.println("File ID: " + file.getId());
    }
}

動作していますが、ユーザーはブラウズからコードをコピーしてコンソールに貼り付ける必要があります。ブラウズからコードをコピーせずに作成する可能性はありますか? このようなものを見つけました ( https://developers.google.com/drive/web/service-accountsから)

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)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}

しかし、私は例外を取得しています:

Exception in thread "main" java.io.IOException: toDerInputStream rejects tag type 56

何が間違っているのか理解できず、見つけることができません。

クレデンシャルを扱う作業が非常に難しく、非常に単純なアプリケーションを作成できないことに本当に驚いています。

4

1 に答える 1

0

プロジェクトで「SERVICE_ACCOUNT_PKCS12_FILE」を生成する必要があるサービス アカウントを使用している場合は、サービス アカウントで生成される SERVICE_ACCOUNT_EMAIL を提供する必要があり、キー ファイルをダウンロードしてパス (SERVICE_ACCOUNT_PKCS12_FILE_PATH) を提供する必要があります。その非常に簡単です。

于 2014-09-24T11:17:01.883 に答える