AuthorizationCodeInstalledApp

时间:2018-07-04 22:45:50

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

我正尝试使用我运行的google drive.api

private static Credential authorize() throws Exception {
        // load client secrets
        InputStream in = new FileInputStream("C:\\Users\\orion\\OneDrive\\Documents\\GitHub\\teachervoiceorganization\\JavaProject\\client_id.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(in));
        if (clientSecrets.getDetails().getClientId().startsWith("515427348790")
                || clientSecrets.getDetails().getClientSecret().startsWith("i50nkSMoqVegC0UdkD1W8g3Y")) {
            System.out.println(
                    "Enter Client ID and Secret from https://code.google.com/apis/console/?api=drive "
                            + "into drive-cmdline-sample/src/main/resources/client_secrets.json");
        }
        // set up authorization code flow
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, JSON_FACTORY, clientSecrets,
                Collections.singleton(DriveScopes.DRIVE_FILE)).setDataStoreFactory(dataStoreFactory)
                .build();
        // authorize
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

我遇到了问题

return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

用户ID在哪里?我看过https://console.developers.google.com,没有运气。任何事情都会有所帮助。

1 个答案:

答案 0 :(得分:0)

Google用户ID编码为从调用返回的JWT的sub,以将Auth Code交换为令牌。您正在使用混淆所有内容的Java客户端库,因此任何人都可以猜测JWT令牌的公开位置。我的个人建议是丢弃该库,而直接直接调用两个OAuth URL。 步骤是:-

  1. 构造包含您的客户端ID,范围和回调URL的OAuth请求URL
  2. 重定向到该URL
  3. 当浏览器重定向回您的回调Servlet时,调用令牌端点以请求包含身份的令牌。

https://developers.google.com/identity/protocols/OAuth2WebServer

对此的描述非常好。

Nb,您需要包括email范围和适当的Drive范围。