Google Drive API 在服务器浏览器上显示同意屏幕

问题描述 投票:0回答:2

所以这是我用来连接到 Google Drive API 并验证我的用户的代码,我面临的问题是,每当我调用该函数来验证用户时,同意屏幕就会显示在服务器上(其中 tomcat安装的,而不是用户正在使用的 PC)。我知道这是一个奇怪的问题,我在论坛中找不到任何相关内容。

private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {

    final String CREDENTIALS_FILE_PATH = "/credentials.json";
    final String TOKENS_DIRECTORY_PATH = "/tokens";
    final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

    InputStream in = this.getClass().getResourceAsStream(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(9999).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

调用此方法后,应用程序将打开服务器的默认浏览器(Chrome)并显示同意屏幕。我不知道我在这里做错了什么。谢谢你的帮助。干杯!

java oauth-2.0 google-drive-api google-oauth google-api-java-client
2个回答
2
投票

GoogleAuthorizationCodeFlow.Builder
旨在由已安装的应用程序使用。

你应该使用的是

GoogleBrowserClientRequestUrl

public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
  String url = new GoogleBrowserClientRequestUrl("812741506391.apps.googleusercontent.com",
      "https://oauth2.example.com/oauthcallback", Arrays.asList(
          "https://www.googleapis.com/auth/userinfo.email",
          "https://www.googleapis.com/auth/userinfo.profile")).setState("/profile").build();
  response.sendRedirect(url);
}

#client-side_applications


0
投票

??? 403:disallowed_useragent 选项: access_type=offline Response_type=code redirect_uri=http://localhost:38677 client_id=833042598497.apps.googleusercontent.comscope=https://www.googleapis.com/auth/drive https://www.googleapis 。 com/auth/userinfo.profile = 同意 阿尔巴尼亚 阿尔巴尼亚 阿尔巴尼亚 阿尔巴尼亚

© www.soinside.com 2019 - 2024. All rights reserved.