Rails:使用RestClient进行外部API集成(400错误请求) [英] Rails: External API Integration using RestClient (400 Bad Request)

查看:130
本文介绍了Rails:使用RestClient进行外部API集成(400错误请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个数字图书馆,并且我已经完成了许多所需的功能.我目前在将数字图书馆与学习管理系统(LMS)集成时遇到问题.

I am building a digital library, and I have completed a lot of the functionalities needed. I am currently having an issue with integrating the digital library with a Learning Management System (LMS).

我已经有了使用 Devise gem 的数字图书馆的管理员身份验证系统.我的目标是允许希望访问数字图书馆的用户使用他们的学习管理系统(LMS)凭据(用户名和密码)登录数字图书馆.

I already have an admin authentication system for the digital library using the Devise gem. My goal is to allow users who want to access the digital library to login to the digital library using their Learning Management System (LMS) credentials (username and password).

已经为我提供了Login API端点和学习管理系统(LMS)的其他所需参数,并且我已经创建了用户模型,即会话控制器会话视图模板.

I have been provided with the Login API endpoint and other needed parameters of the Learning Management System (LMS), and I have created the User Model, the Sessions Controller and the Sessions View Templates.

我目前正在使用 RestClient Gem 进行API调用,但是出现错误 400错误请求.我尝试调试该问题,它使我指向 Sessions Controller

I am currently using the RestClient Gem for the API call, but I having an error 400 Bad Request. I have tried to debug the issue and it gets to point me to this line in the Sessions Controller

 response = RestClient::Request.execute(

下面是我的源代码

会话控制器

require 'rest-client'

class SessionsController < ApplicationController
  def new
  end

  def create
    response = RestClient::Request.execute(
      method: :post,
      url: 'https://newapi.example.com/token',
      payload: { 'username': 'params[:username]',
                 'password': 'params[:password]',
                 'grant_type':'password' },
      headers: { apiCode: '93de0db8-333b-4f478-aa92-2b43cdb7aa9f' }
    )

    case response.code
    when 400
      flash.now[:alert] = 'Email or password is invalid'
      render 'new'
    when 200
      session[:user_id] = user.id
      redirect_to root_url, notice: 'Logged in!'
    else
      raise "Invalid response #{response.to_str} received."
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, notice: 'Logged out!'
  end
end

会话新视图

<p id="alert"><%= alert %></p>
<h1>Login</h1>
<%= form_tag sessions_path do %>
  <div class="field">
    <%= label_tag :username %>
    <%= text_field_tag :username %>
  </div>
  <div class="field">
    <%= label_tag :password %>
    <%= password_field_tag :password %>
  </div>
  <div class="actions">
    <%= submit_tag 'Login' %>
  </div>
<% end %>

用户模型

class User < ApplicationRecord
  has_secure_password

  validates :username, presence: true, uniqueness: true
end

任何形式的代码示例帮助将不胜感激.如果需要,我也愿意提供有关此集成的更多信息.预先谢谢你.

Any form of help with code samples will be greatly appreciated. I am also open to providing more information about this integration if required. Thank you in advance.

推荐答案

您应该删除参数周围的引号:

You should remove quote around your params:

 response = RestClient::Request.execute(
      method: :post,
      url: 'https://newapi.example.com/token',
      payload: { 'username': params[:username],
                 'password': params[:password],
                 'grant_type':'password' },
      headers: { apiCode: '93de0db8-333b-4f478-aa92-2b43cdb7aa9f' }
    )

如果带引号,则表示您的username值和password值是硬编码的.在这里,您想从参数中检索usernamepassword.

If you have quotes it's means that your username value et password value are hard-coded..here you want to retrieve username and password from your params.

这篇关于Rails:使用RestClient进行外部API集成(400错误请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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