1
我寫了一些代碼,在Google驅動器中成功上傳圖片,但我需要將瀏覽器中的授權代碼複製到瀏覽器中,然後運行此項目並在瀏覽器中獲取代碼。我需要在控制檯中複製這段代碼,然後工作正常。如何在java中使用批處理程序上傳文件到Google Drive?
但我想爲批量文件上傳圖像,所以不要這些手動工作。 請幫幫我。 在此先感謝。
這裏是我的代碼:
/**
* 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());
你可以給我**代碼**的例子刷新令牌。我無法找到它。 **感謝** – user1878119
請查看此頁面以瞭解所有與授權相關的摘錄:https://developers.google.com/drive/credentials –