简体   繁体   English

无需用户交互的 Google Cloud OAuth 2.0 请求令牌 - Java

[英]Google Cloud OAuth 2.0 Request Token without user interaction - Java

I am working on a solution to read log files from the GCP for an internal process.我正在研究从 GCP 读取日志文件以用于内部进程的解决方案。 However, i am having a difficult time trying to generate an Auth Token for the request to grab the logs needed.但是,我在尝试为获取所需日志的请求生成身份验证令牌时遇到了困难。 This is more of a flow\\context question rather than a whats wrong with my code one这更像是一个流程\\上下文问题,而不是我的代码有什么问题

The key issues i am having is that i do not want to prompt for web-browser authentication.我遇到的关键问题是我不想提示进行网络浏览器身份验证。 I want to be able to do this all through API request and have no user interaction.我希望能够通过 API 请求完成这一切并且没有用户交互。 Everywhere i have looked and all implementations i have tried, i am prompt for user interaction in some way and that is just not feasible for this solution.我看过的每一个地方和我尝试过的所有实现,我都会以某种方式提示用户交互,但这对于这个解决方案是不可行的。

How can this be achieved?如何做到这一点?

We do not have IAM enabled, so i cannot generate a JWT token.我们没有启用 IAM,所以我无法生成 JWT 令牌。 I am trying to do this through using a Service Account created using client id and client secret.我试图通过使用使用客户端 ID 和客户端密码创建的服务帐户来做到这一点。

I have tried getting a "code" to pass into a request to generate an authorization token, but that has been prompting me for user authorization in the browser which will not work, even when I add the query parameter 'prompt' or 'approval_prompt' to none or force.我曾尝试将“代码”传递到生成授权令牌的请求中,但这一直提示我在浏览器中进行用户授权,即使我添加了查询参数“prompt”或“approval_prompt”,这也不起作用无或强制。 I feel like i am missing one crucial piece to be able to achieve this flow and any help/guidance will be greatly appreciated.我觉得我缺少一个能够实现这一流程的关键部分,任何帮助/指导将不胜感激。

There are several ways to authenticate API calls.有多种方法可以验证 API 调用。 If you want to do it without user interaction, you will need to use a Service Account (more info here ).如果您想在没有用户交互的情况下执行此操作,则需要使用服务帐户( 此处有更多信息)。 The process would be the following:该过程如下:

You use the client ID and one private key to create a signed JWT and construct an access-token request in the appropriate format.您使用客户端 ID 和一个私钥来创建签名的 JWT 并以适当的格式构建访问令牌请求。 Your application then sends the token request to the Google OAuth 2.0 Authorization Server, which returns an access token.然后,您的应用程序将令牌请求发送到 Google OAuth 2.0 授权服务器,后者返回访问令牌。 The application uses the token to access a Google API.应用程序使用令牌访问 Google API。 When the token expires, the application repeats the process.当令牌过期时,应用程序重复该过程。

For this, you can use Client Libraries or you can do it manually with HTTP requests directly.为此,您可以使用客户端库,也可以直接使用 HTTP 请求手动完成。 In the docs there is a guide to do so.文档中有一个指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM