Googleドライブに画像を正常にアップロードするコードをいくつか書きましたが、このプロジェクトを実行してブラウザでコードを取得した後に取得した認証コードをブラウザでコピーする必要があります。このコードをコンソールにコピーしてから、正常に動作する必要があります。
しかし、画像をアップロードするためのバッチファイルを作成しようとしているので、これらの手動作業は必要ありません。私を助けてください。前もって感謝します。
これが私のコードです:
/**
* Client id from GD.
*/
private static String CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
/**
* Client secret key from GD.
*/
private static String CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
/**
* Redirect url from GD.
*/
private static String REDIRECT_URI = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
public static void main(String[] args) throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
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);
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);
//Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
//Insert a file
File body = new File();
body.setTitle("imagename");
body.setDescription("A test document");
body.setMimeType("image/jpeg");
java.io.File fileContent = new java.io.File("icon.jpg");
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());