从 Rails 调用 Uber API:{“error":“invalid_client"} [英] Calling Uber API from Rails: {"error": "invalid_client"}

查看:84
本文介绍了从 Rails 调用 Uber API:{“error":“invalid_client"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此待了一天.

使用 Rails 调用 Uber API 并且无法获取访问令牌.获取授权码有效,但交换访问令牌无效.

Using Rails to call the Uber API and failing to get an access token. Grabbing the authorization code works, but exchanging for an access token does not.

我尝试过使用和不使用 OAuth 2.0 gem,并确保我的所有密钥都准确无误.也尝试了两个单独的 Uber 帐户.所有组合都给出相同的错误:{"error": "invalid_client"}.

I've tried with and without the OAuth 2.0 gem and made sure all my keys were accurate. Tried on two separate Uber accounts, too. All combinations give the same error: {"error": "invalid_client"}.

我将在下面发布非 OAuth 代码.params[:code] 是优步返回的验证码.

I'll post the non-OAuth code below. params[:code] is the auth code returned from Uber.

uri = URI.parse('https://login.uber.com/oauth/v2/token')
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true

headers = {
  # authentication content-type is not json
  # 'Content-Type'  => 'application/x-www-form-urlencoded',
  'Authorization' => 'Token ' + @server_token
}

request = Net::HTTP::Post.new(uri.path, headers)

request.set_form_data({
  'client_id'     => @client_id,
  'client_secret' => @client_secret,
  'grant_type'    => 'authorization_code',
  'code'          => params[:code]
})

response = https.request(request)

render :json => response.body

预先感谢您的帮助.

推荐答案

  • 如果您收到 {"error": "invalid_client"} 那么这意味着您正在发送一个额外的 HTTP 请求标头,该标头不是必需的,但已由 Uber OAuth 提供商服务器验证.
    另一个可能的原因可能是您拼错了请求参数名称.(不是你的情况)

    • If you get {"error": "invalid_client"} then this means you are sending an additional HTTP request header which is not required but is validated by the Uber OAuth provider server.
      Another possible reason could be that you misspelled the request parameter names. (not your case)

      解决方案:您需要从请求中删除 Authorization 标头并重试,即不要发送任何 HTTP 请求标头.

      Solution: You need to remove the Authorization header from your request and try again, i.e. don't send any HTTP request headers.

      如果您在 Uber Developers Dashboard 中定义了多个重定向 URL,并且您在没有一个人的情况下发出身份验证请求,请记住使用了在仪表板中定义的第一个 URL:

      If you have multiple redirect URLs defined in the Uber Developers Dashboard and you are making an authentication request without one keep in mind that the first URL defined in the dashboard is used:

      如果未提供,则默认为第一个重定向 URI应用程序的仪表板.

      If none is provided the default is the first redirect URI provided in the application's dashboard.

      如果第一个 URL 与您在 OAuth 流程的授权步骤 你会得到 {"error": "access_denied"}

      and if that first URL doesn't match the URL you used in the authorize step of the OAuth flow you will get {"error": "access_denied"}

      但是,我注意到即使第一个 URL 与使用授权步骤发送的 URL 匹配也会引发此错误,因此我建议您在 OAuth 流程的每个步骤发送 redirect_uri 参数.

      However, I've noticed that this error is raised even if the first URL matches the URL sent with the authorize step, so I recommend you send the redirect_uri parameter at each step of the OAuth flow.

      解决方案:您需要在请求中发送redirect_uri参数(与您在授权步骤中使用的相同)

      Solution: You need to send the redirect_uri parameter in the request (the same that you used on the authorize step)

      如果 params[:code] 中的代码已经被使用过一次或者已经失效,你会得到 {"error": "invalid_grant"}

      If the code inside params[:code] has already been used once or has become invalid you get {"error": "invalid_grant"}

      解决方案:您需要重做 OAuth 流程中的授权(第一步)步骤,以获取您需要交换访问令牌的新代码.

      Solution: You need to redo the authorize (first) step in the OAuth flow to obtain a new code which you need to exchange for an access token.

      这篇关于从 Rails 调用 Uber API:{“error":“invalid_client"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆