import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.lang.Object;
import java.util.ArrayList;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
public class DriveQuickstart {
private static final String APPLICATION_NAME = "DriveQuickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
// * Global instance of the scopes required by this quickstart.
// * If modifying these scopes, delete your previously saved tokens/ folder.
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
private static final String CREDENTIALS_FILE_PATH = "C:/Quickstart/src/main/resources/client_secret.json";
// * Creates an authorized Credential object.
// * @param HTTP_TRANSPORT The network HTTP Transport.
// * @return An authorized Credential object.
// * @throws IOException If the client_secret.json file cannot be found.
// */
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public static void main(String[] args) throws IOException, GeneralSecurityException {
try
{
// Build a new authorized API client service.
System.out.println("This is a test line");
//declare final variable NetHttpTransport and GoogleNetHttpTransport from import statements
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
System.out.println("This is the second test line");
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
System.out.println("This is the third test line");
// Print the names and IDs for files.
FileList result = service.files().list()
.setPageSize(10)
.setFields("nextPageToken, files(id, name)")
.execute();
List<File> files = result.getFiles();
if (files == null || files.isEmpty()) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getName(), file.getId());
}
}
Drive.Files var1 = service.files();
System.out.println("var1 = " + var1.toString());
Drive.Files.List var2 = var1.list();
if (var2 == null)
System.out.println("null");
else {
System.out.printf("Var2 size is: %d\n", var2.size() );
}
//iterate through map and print results
}
catch(Exception e){
System.out.println("error");
}
System.out.println("This is the fourth test line");
}
}
}
更详细地说。我正在用一种有点非常规的方法来尝试。我只使用命令行来运行(没有IDE,没有gradle)。
这里有一个链接到一个ss与我的输出在命令提示符。cmd输出
假设我实际上是在访问Google Drive中的元数据,并且Drive.files嵌套在抽象映射(java.util.AbstractMap)中,我应该能够返回映射的大小,从而通过返回多少文件来了解它在哪里搜索。
我不确定是怎么回事。第一次编译和运行时,我在chrome中打开了一个选项卡,要求获得访问要驱动的元数据的权限。第二次,它会给出“警告:无法更改每个人的权限:”错误。除此之外,var1('set to service.files()')在使用toString()时返回com.google.api.services.drive.drive$files@458ad742。分配给var2的子类service.files().list()应该返回我的Google驱动器中的文件列表,但显然它没有这样做。
我可能误解了很多东西,所以请原谅我。谢谢你的帮助。
StoredCredential
因此,仅从这一点来说,我(认为)连接到谷歌驱动器。但是,默认的代码应该列出10个文件的名称和ID,它什么都不能做。
如何运行程序:下面是我用来编译和运行CMD代码的批处理文件。
jar cfe DriveQuickstart.jar Quickstart DriveQuickstart.class
javac -cp ./* DriveQuickstart.java
java -cp ./* DriveQuickstart
出于某种原因,我不知道/理解,这是不从导入的不存在的jar文件返回方法的唯一方法。
com.google.api.services.drive.Drive$Files@458ad742
<NAMESPACE>$<CLASS>@<HASHCODE>
下面是https://github.com/google/google-api-nodejs-client的代码。 一般问题:刷新令牌实际上如何与访问令牌一起工作? 背景:根据我的解释,每个访问令牌都有一个有限的时间跨度(~1小时)。因此,当用户第一次连接到我的服务器(服务器为用户身份验证提供了机制)时,服务器将收到有限生命期访问令牌和一次性刷新令牌。1小时后,访问令牌过期。 谢了!
这是我收到的错误
我正在使用google drive api来管理多个用户的一些文件。现在我想知道如何将一些文件的所有权转移给另一个用户。
当观察驱动器更改时,我会收到带有新更改ID的通知。但是当我尝试使用:driveservice.changes().get(changeId)查询它时,我断断续续地得到404。我是不是做错什么了? 正在观看文件: 当观察文件更改时,如果是文件夹,我想知道添加到该文件夹的新文件,所以我希望从该文件夹添加/删除文件时,“x-goog-resource-state”将包含“add/remove”值,而“x