我们正计划在谷歌云上主持一个主题(pub / sub),并创build了一个主题和客户服务帐户。
现在我必须连接到主题并发布消息。 我在发布消息之前尝试进行身份validation。 但是对于我的场景“本地服务器到使用其他API的Google云身份validation”找不到合适的方法。
请帮助我,如果有任何方法可用这种方法。
这里是我使用的代码,但给浏览器redirect响应,而不是访问令牌。 请find下面的代码和回复
private static Credential authorize() throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleClientSecrets clientSecrets = JSON_FACTORY.fromReader(new InputStreamReader(new FileInputStream(new File(KEY_FILE_LOCATION))), GoogleClientSecrets.class); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret in client_secrets.json"); System.exit(1); } GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(PubsubScopes.PUBSUB)).build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(SERVICE_ACCOUNT_EMAIL); } Please open the following address in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=1164246778127&redirect_uri=http://localhost:53869/Callback&response_type=code&scope=https://www.googleapis.com/auth/pubsub
所有你需要的是服务帐户的JSON密钥文件。 查看文档以了解如何使用这个(使用GOOGLE_APPLICATION_CREDENTIALS环境variables的应用程序默认凭据)。