【问题标题】:"401 Unauthorized" when trying to watch changes on Google Drive with Java API Client尝试使用 Java API 客户端查看 Google Drive 上的更改时出现“401 Unauthorized”
【发布时间】:2014-03-04 13:52:23
【问题描述】:

真的卡在这里了。代码由 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)

【问题讨论】:

  • 你在代理服务器后面吗?
  • 不在代理后面...
  • 您能否将http请求和响应转储到您的问题中
  • 您确定您的令牌/密钥有效吗?
  • 我遇到了同样的错误...这个问题解决了吗?

标签: java google-api google-drive-api google-api-java-client


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    以下步骤可能会帮助您解决问题:

    -查看您的 Google Apps 帐户,可能您的 Docs API 访问权限已关闭。

    -尝试验证您的令牌和密钥并重定向 url。

    【讨论】:

      【解决方案3】:

      credential.getRequestInitializer() 是否为空,检查一下

      阅读这篇文章 101% 帮助你

      Get Userinfo from Oauth2 Google Contacts API

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 1970-01-01
        • 2017-10-30
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多