使用 java 程序连接到我的 google 驱动器时函数中传递的值应该是多少?
what should be the value passed in function to connect to my google drive using java program?
我从 google 开发者网站获得了以下代码:
package javaapplication24;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import java.io.IOException;
import java.io.InputStream;
// ...
public class NewClass {
// ...
/**
* Print a file's metadata.
*
* @param args
* @param service Drive API service instance.
* @param fileId ID of the file to print metadata for.
*/
private static void printFile(Drive service, String fileId) {
try {
File file = service.files().get(fileId).execute();
System.out.println("Title: " + file.getTitle());
System.out.println("Description: " + file.getDescription());
System.out.println("MIME type: " + file.getMimeType());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
/**
* Download a file's content.
*
* @param service Drive API service instance.
* @param file Drive File instance.
* @return InputStream containing the file's content if successful,
* {@code null} otherwise.
*/
private static InputStream downloadFile(File file, Drive service) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
// ...
}
我知道我需要用它编写一个主函数并调用函数 printFile 和 DownloadFile 但我没有得到作为变量服务在函数中传递的内容?
你必须先了解它是如何工作的!
- 为了下载或打印有关您的文件的详细信息,您需要从您的 google 帐户对您的应用程序进行身份验证,并具有
scope
(只读、modify/delete 等权限) ).
- 身份验证完成后。您将获得访问令牌以访问数据。
这是一小段代码,说明了 Drive service
的含义
/**
* Build and return an authorized Drive client service.
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
前往此 link 了解更多信息
https://developers.google.com/drive/web/quickstart/java
注意:如果我在此处复制粘贴完整代码,答案会不必要地长
我从 google 开发者网站获得了以下代码:
package javaapplication24;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;
import java.io.IOException;
import java.io.InputStream;
// ...
public class NewClass {
// ...
/**
* Print a file's metadata.
*
* @param args
* @param service Drive API service instance.
* @param fileId ID of the file to print metadata for.
*/
private static void printFile(Drive service, String fileId) {
try {
File file = service.files().get(fileId).execute();
System.out.println("Title: " + file.getTitle());
System.out.println("Description: " + file.getDescription());
System.out.println("MIME type: " + file.getMimeType());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
/**
* Download a file's content.
*
* @param service Drive API service instance.
* @param file Drive File instance.
* @return InputStream containing the file's content if successful,
* {@code null} otherwise.
*/
private static InputStream downloadFile(File file, Drive service) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
// ...
}
我知道我需要用它编写一个主函数并调用函数 printFile 和 DownloadFile 但我没有得到作为变量服务在函数中传递的内容?
你必须先了解它是如何工作的!
- 为了下载或打印有关您的文件的详细信息,您需要从您的 google 帐户对您的应用程序进行身份验证,并具有
scope
(只读、modify/delete 等权限) ). - 身份验证完成后。您将获得访问令牌以访问数据。
这是一小段代码,说明了 Drive service
的含义
/**
* Build and return an authorized Drive client service.
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
前往此 link 了解更多信息
https://developers.google.com/drive/web/quickstart/java
注意:如果我在此处复制粘贴完整代码,答案会不必要地长