【问题标题】:OAuth2.0 token post request is responding 401 (invalid_client)OAuth2.0 令牌发布请求响应 401 (invalid_client)
【发布时间】:2020-07-28 02:56:20
【问题描述】:

我对 Google OAUth2.0 身份验证非常陌生,因此我的问题听起来很愚蠢。但是,我被这个问题困扰了很长时间,需要您的意见来解决它。

我在 my app 中集成了 Globus 登录。 Globus 登录使用 Google OAuth-2 协议进行身份验证。根据Globus Auth developer guide,我成功将应用重定向到他们的授权服务,用户可以将他们的凭据进行身份验证,并且应用在成功验证后接收从Globus Auth服务器返回的代码。下一步是将代码发送到 Token 端点以获取访问令牌。我使用了以下代码:

var querystring = require('querystring');    
export const logInGlobus = (payload) => {
let tokenUri = encodeURIComponent(payload.redirect_uri);
let client_id = 'out app client id'
let client_secret = 'client secret generated for authentication'
let cred = btoa(client_secret);

return axios.post('https://auth.globus.org/v2/oauth2/token',
querystring.stringify({
    grant_type: 'authorization_code',
    code: payload.code,
    redirect_uri: tokenUri,
    client_id: client_id
}),
{
    headers:{
        Authorization: 'Basic '+ cred,
        'Content-Type': 'application/x-www-form-urlencoded'
      }
})
 .then(response => {
     return{
         res: response,
         success: true
     }
 })
 .catch(err => {
     return{
         res: err,
         success: false
     }
 })

}

我收到来自服务器的此发布请求的 401 {"error":"invalid_client"} 代码。我错过了什么?

N.B:我尝试过不使用客户端密码、客户端 ID、不编码重定向 URL。到目前为止没有运气>

如果您能给我一些启发,我将非常感谢您的努力。感谢您的宝贵时间。

====编辑====

附加浏览器控制台的错误

【问题讨论】:

  • 能否附上具体错误的截图
  • 附加了来自控制台的错误。您可以通过在 p2irc-data-dev.usask.ca 上单击我的应用程序窗口顶部的登录按钮,然后单击 Globus 登录并在 Globus 执行身份验证来重现该错误。

标签: javascript oauth-2.0 axios google-oauth http-status-code-401


【解决方案1】:

我解决了这个问题。我不得不将客户端密码放在发布请求的正文中。下面的代码解决了我的问题。

var querystring = require('querystring');

export const logInGlobus = (payload) => {
let client_id = 'app client id'
let client_secret = 'client secret generated for authentication'

return axios.post('https://auth.globus.org/v2/oauth2/token',
querystring.stringify({
    grant_type: 'authorization_code',
    code: payload.code,
    redirect_uri: payload.redirect_uri,
    client_id: client_id,
    client_secret: client_secret
}),
{
    headers:{
        'Content-Type': 'application/x-www-form-urlencoded'
      }
})
 .then(response => {
     return{
         res: response,
         success: true
     }
 })
 .catch(err => {
     return{
         res: err,
         success: false
     }
 })

}

【讨论】:

    猜你喜欢
    • 2013-06-14
    • 2011-04-10
    • 1970-01-01
    • 2018-02-18
    • 2019-02-21
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多