gpt4 book ai didi

Java google drive api 凭据

转载 作者:行者123 更新时间:2023-11-30 10:50:00 24 4
gpt4 key购买 nike

我想使用我存储的凭证文件,请帮助我。桌面应用程序。我不明白如何使用保存的凭据。我在谷歌支持上找不到一些说明。

private static final String APPLICATION_NAME
= "edocsAPI";
private static final java.io.File DATA_STORE_DIR = new java.io.File(
"E:/", ".credentials/drive-java-quickstart");
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final JsonFactory JSON_FACTORY
= JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
private static final List<String> SCOPES
= Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
}
}

public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in
= new FileInputStream("E:/client_secret_Rep.json");
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(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential
= new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");

return credential;
}

public static Drive getDriveService() throws IOException {
Credential credential =
authorize();
System.out.println("getAccessToken" + credential.getAccessToken());

System.out.println("setAccessToken" + credential.getAccessToken());
System.out.println("setExpiresInSeconds" + credential.getExpiresInSeconds());

System.out.println("setRefreshToken" + credential.getRefreshToken());
return new Drive.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}

public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Drive service = getDriveService();
FileList result = service.files().list()
.setMaxResults(10)
.execute();
List<File> files = result.getItems();
if (files == null || files.size() == 0) {
System.out.println("No files found.");
} else {
System.out.println("Files:");
for (File file : files) {
System.out.printf("%s (%s)\n", file.getTitle(), file.getId() , file.getAlternateLink());
file.setShared(Boolean.TRUE);
System.out.println(file.getAlternateLink());
}
}

}

我尝试这样做:

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets("Client ID",
"ClientSecrets").build();

credential.setAccessToken("AccessToken");

但是我有一个错误 401

最佳答案

看起来错误是由于访问 token 过期或无效导致的。

Invalid authorization header. The access token you're using is either expired or invalid.

这是 documentation 的内容提示大约 401 错误。

Suggested action: Refresh the access token using the long-lived refresh token. If this fails, direct the user through the OAuth flow, as described in Authorizing Your App with Google Drive.

关于Java google drive api 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com