gpt4 book ai didi

java - "401 Unauthorized"尝试使用 Java API 客户端查看 Google Drive 上的更改

转载 作者:搜寻专家 更新时间:2023-11-01 00:56:45 25 4
gpt4 key购买 nike

真的卡在这里了。根据 Google 提供的示例构建的代码:

public static void main(String[] args) {
try {
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).setApplicationName("Google Push").build();

//Insert a file
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");

java.io.File fileContent = new java.io.File("document.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(fileContent));
bw.write("allo!!!!");
bw.close();

System.out.println("file created? -> " + fileContent.createNewFile());
FileContent mediaContent = new FileContent("text/plain", fileContent);

File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());

watchChange(service, "channelId", "web_hook", "https://clementlevallois.net/notifications"); // line 78

} catch (IOException ex) {
Logger.getLogger(DriveCommandLine.class.getName()).log(Level.SEVERE, null, ex);
}

}

private static Channel watchChange(Drive service, String channelId, String channelType, String channelAddress) {

Channel channel = new Channel();
channel.setId(channelId);
channel.setType(channelType);
channel.setAddress(channelAddress);
try {
return service.changes().watch(channel).execute(); //line 91
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

堆栈跟踪:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at Controller.DriveCommandLine.watchChange(DriveCommandLine.java:91)
at Controller.DriveCommandLine.main(DriveCommandLine.java:78)

最佳答案

API 在调用 Drive API 时返回 HTTP 401 或 HTTP 403 响应。这些错误可能表明:

token 到期, token 撤销,(这将导致访问 token 和刷新 token 都停止工作), token 未授权所需范围,未使用 OAuth 2.0 协议(protocol)正确授权请求。

token 过期可以通过调用 refreshToken() 来处理。如果该调用失败并显示“无效凭据”

关于java - "401 Unauthorized"尝试使用 Java API 客户端查看 Google Drive 上的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173832/

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