如何用java编写批处理程序,以便在Google Drive中上载文件?
我已经写了一些代码,是上传图像成功地在谷歌驱动器,但我需要复制在浏览器中的授权代码,我们运行这个项目后,在浏览器中得到的代码。我需要在控制台中复制此代码,然后它就可以正常工作了 但是我正在尝试制作用于上传图像的批处理文件,所以不需要这些手工工作。 请帮帮我。 提前谢谢 这是我的密码:如何用java编写批处理程序,以便在Google Drive中上载文件?,java,google-docs,google-docs-api,google-drive-api,Java,Google Docs,Google Docs Api,Google Drive Api,我已经写了一些代码,是上传图像成功地在谷歌驱动器,但我需要复制在浏览器中的授权代码,我们运行这个项目后,在浏览器中得到的代码。我需要在控制台中复制此代码,然后它就可以正常工作了 但是我正在尝试制作用于上传图像的批处理文件,所以不需要这些手工工作。 请帮帮我。 提前谢谢 这是我的密码: /** * Client id from GD. */ private static String CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/**
* 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());
您只需在第一次运行应用程序时通过授权流程。然后,您可以存储刷新令牌并使用它构建授权请求所需的凭据对象。刷新令牌的时间持续到用户手动撤销为止。您能给我发送刷新令牌的代码示例吗。我找不到它。感谢检查此页面中所有与授权相关的代码段: