【问题标题】:Oauth through curl通过 curl 进行 Oauth
【发布时间】:2021-10-31 12:56:22
【问题描述】:

我通过谷歌搜索并使用关键字,例如site:stackoverflow.com,但我没有找到解决我问题的相关解决方案。我想这是因为有几个问题碰撞在一起。

我的问题: 我已经部署了 Google Cloud Functions。我可以成功地使用 gcloud 获取识别令牌(通过步骤 1. gcloud auth login 2. gcloud set project 3. gcloud auth print-identity-token)来访问部署的 Google Cloud Functions 没有问题。

但是,有一个新要求是外部客户端也希望通过 curl(或 RESTful 纯 http 样式)访问该 Google Cloud Functions。现在问题出现了。在点击链接 [1] 时,

  • 我不知道 client_id 和 client_secret,因为部署的 Google Cloud Functions 是一个服务帐户,看起来没有 client_id 和 client_secret。我尝试使用在旧凭证 adc.json 中找到的 client_id 和 client_secret。但是 curl 会响应一个 html 文档,例如

    文档已移动here

我可以通过单击浏览器中的链接来进行身份验证,但这错过了以非交互方式进行身份验证的目的。

  • 不知道刷新到令牌的确切参数,但我想这是因为我没有使用正确的 client_id 和 client_secret。

感谢任何建议和评论。非常感谢!

[1]。 https://developers.google.com/identity/protocols/oauth2/web-server

【问题讨论】:

标签: oauth-2.0 oauth google-oauth


【解决方案1】:

这只是一种解决方法,但它适用于我当前的要求。我知道这可能不是正确的答案。但如果有人遇到类似问题,我希望它会有所帮助。

  1. 创建client_id、client_secret和refresh_token

    gcloud auth application-default print-access-token
    
  2. 刷新令牌

    curl -s https://oauth2.googleapis.com/token  \
        -d client_id=$client_id \
        -d client_secret=$client_secret \
        -d refresh_token=$refresh_token \
        -d grant_type=refresh_token
    

第一步将创建一个application-default.json 文件,该文件将client_idclient_secretrefresh_token 存储在gcloud 设置的文件夹中。

然后在第 2 步,可以通过抓取响应 json 中的值来检索刷新的令牌,并将其用于调用部署在 Google env 中的云函数。

我用作参考的文档是https://developers.google.com/identity/protocols/oauth2/web-server#httprest_7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2012-07-19
    • 2010-12-25
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多