首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试使用Java API Client查看Google Drive上的更改时出现"401 Unauthorized“

尝试使用Java API Client查看Google Drive上的更改时出现"401 Unauthorized“
EN

Stack Overflow用户
提问于 2014-03-04 21:52:23
回答 3查看 7.1K关注 0票数 5

真的困在这里了。代码由Google提供的示例构建而成:

代码语言:javascript
运行
复制
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;
}

堆栈跟踪:

代码语言:javascript
运行
复制
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)
EN

回答 3

Stack Overflow用户

发布于 2014-03-13 17:53:02

调用驱动器API时返回HTTP 401或HTTP 403响应的API。这些错误可能表示以下任一项:

令牌过期、令牌吊销(这将导致访问令牌和刷新令牌都停止工作)、未针对所需范围授权的令牌、未使用OAuth 2.0协议正确授权的请求。

可以通过调用refreshToken()来处理令牌过期。如果调用失败,并显示"Invalid Credentials“

票数 1
EN

Stack Overflow用户

发布于 2014-03-12 23:09:28

以下是一些可能帮助您解决问题的步骤:

-Look进入你的Google Apps账户可能你的Docs API访问被关闭了。

-Try验证你的令牌和密钥,然后你重定向url。

票数 0
EN

Stack Overflow用户

发布于 2014-03-14 00:00:48

credential.getRequestInitializer()为空,请检查

阅读这篇文章101%帮助你

Get Userinfo from Oauth2 Google Contacts API

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22173832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档