【问题标题】:how to use express post method with google Oauth2.0 during authorized redirection URL如何在授权重定向 URL 期间使用带有 google Oauth 2.0 的 express post 方法
【发布时间】:2018-05-15 06:27:24
【问题描述】:

我是一个正在实践谷歌oauth2.0的人。我正在创建一个示例,但它不适用于 Oauth 身份验证。

首先,index.html中的操作如下。使用 href 命令请求带有简单标记的 Oauth“请求令牌”。

<a href="https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&access_type=offline&
include_granted_scopes=true&state=state_parameter_passthrough_value&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2FreceiveCode&response_type=code&client_id=665922514832-f7n0s3chgn41vsojtg4gfl2j7c5a5lfr.apps.googleusercontent.com">Using google API
</a>
<form action='http://localhost:3000/user' method='get'>
    <button name="subject" type="submit" value='THIS 123!@# IS GET!!' href='www.google.com'>get</button>
</form>

这里授权的重定向URI是http://localhost:3000/receiveCode。

我想使用 restful API 而不是使用 php 文件。

到目前为止,它运行良好。问题是登录 Google 网站后我无法使用 Scope。 根据谷歌参考https://developers.google.com/identity/protocols/OAuth2WebServer的第5步,

一个会话被链接到一个授权的重定向URI,然后各种数据如Code值被发送到POST请求一个Access Token

但是,在我设置为路由器的receiveCode中,它始终是GET。

代码如下所示,结果 URL:

http://localhost:3000/receiveCode?state=state_parameter_passthrough_value&code=4/AAAhDsl2wLidOA1Gqq9UzXbeWMe25sYx_8HtXx3_rGgeY9cm5mp2uSvdRBNqF6Da9ScEp0jE10mF_ibibEsM3x4&scope=https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/calendar.readonly#

router.get('/receiveCode', (req, res, next) => {
    //NOT ERROR BUT NOT POST
})

router.post('/receiveCode', (req, res, next) => {
    // ERROR (Internal Server Error)
    // actually... never calling enter post method
})

我不确定这是结构问题还是使用 post 的 API。

你不应该首先在 express 中使用 restAPI 吗?

【问题讨论】:

    标签: rest express oauth-2.0


    【解决方案1】:

    redirect_uri 标识的资源确实应该通过 HTTP GET 方法检索,所以我认为到目前为止一切都按预期为您工作。

    从这里,您应该能够从 express req 变量 params property 中检索处理 /receiveCode 实现所需的查询参数,例如:

    req.params.code
    

    说明

    我们怎么知道GET 是正确的?

    在查看实现示例之前,Google 文档并不是很清楚: https://developers.google.com/identity/protocols/OAuth2WebServer#example

    如果你看看例如那里的 Ruby 示例(摘录如下),您可以看到回调 /oauth2callback 类似于您的 /receiveCode 并且确实使用 GET 实现:

    require 'google/apis/drive_v2'
    require 'google/api_client/client_secrets'
    require 'json'
    require 'sinatra'
    
    enable :sessions
    set :session_secret, 'setme'
    
    get '/' do
      unless session.has_key?(:credentials)
        redirect to('/oauth2callback')
      end
      client_opts = JSON.parse(session[:credentials])
      auth_client = Signet::OAuth2::Client.new(client_opts)
      drive = Google::Apis::DriveV2::DriveService.new
      files = drive.list_files(options: { authorization: auth_client })
      "<pre>#{JSON.pretty_generate(files.to_h)}</pre>"
    end
    
    get '/oauth2callback' do
      client_secrets = Google::APIClient::ClientSecrets.load
      auth_client = client_secrets.to_authorization
      auth_client.update!(
        :scope => 'https://www.googleapis.com/auth/drive.metadata.readonly',
        :redirect_uri => url('/oauth2callback'))
      if request['code'] == nil
        auth_uri = auth_client.authorization_uri.to_s
        redirect to(auth_uri)
      else
        auth_client.code = request['code']
        auth_client.fetch_access_token!
        auth_client.client_secret = nil
        session[:credentials] = auth_client.to_json
        redirect to('/')
      end
    end
    

    对于一些额外的上下文,这里是 Oauth 2 规范的相应部分:

    https://www.rfc-editor.org/rfc/rfc6749#section-4.1.2

    https://www.rfc-editor.org/rfc/rfc6749#section-4.1.1

    https://www.rfc-editor.org/rfc/rfc6749#section-3.1.2

    【讨论】:

    • 谢谢。但是有一个问题。在链接的示例中,我得到一个 GET,将其发送到 post,然后再次重定向。这是一个好的结构吗?
    • 听起来不错。您可以在 developers.google.com/identity/protocols/… 的 HTTP/REST 示例中更好地看到这部分(而不是 Ruby 将其抽象到库中)。查看r = requests.post('https://www.googleapis.com/oauth2/v4/token', data=data) 附近的部分,了解 POST 到令牌端点步骤,该步骤发生在您收到 GET /receiveCode 之后。
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2012-11-12
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多