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

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

问题描述

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

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

我已获得学习管理系统 (LMS) 的登录 API 端点和其他所需参数,并且我创建了用户模型>会话控制器会话视图模板.

我目前正在使用 RestClient Gem 进行 API 调用,但出现错误 400 Bad Request.我试图调试这个问题,它让我指向 Sessions Controller

中的这一行

 response = RestClient::Request.execute(

下面是我的源代码

会话控制器

需要'rest-client'类 SessionsController <应用控制器定义新结尾定义创建响应 = RestClient::Request.execute(方法::发布,url: 'https://newapi.example.com/token',有效载荷:{'用户名':'参数[:用户名]','密码':'参数[:密码]','grant_type':'密码' },标头:{ apiCode:'93de0db8-333b-4f478-aa92-2b43cdb7aa9f'})案例响应.code当 400flash.now[:alert] = '电子邮件或密码无效'呈现新"当 200session[:user_id] = user.idredirect_to root_url,注意:已登录!"别的引发收到无效响应 #{response.to_str}."结尾结尾销毁session[:user_id] = nilredirect_to root_url,注意:登出!"结尾结尾

会话新视图

<%= alert %>

<h1>登录</h1><%= form_tag session_path do %><div class="field"><%= label_tag :用户名 %><%= text_field_tag :用户名%>

<div class="field"><%= label_tag :密码%><%= password_field_tag :password%>

<div class="actions"><%= submit_tag '登录' %>

<%结束%>

用户模型

class User <申请记录has_secure_password验证:用户名,存在:真,唯一性:真结尾

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

解决方案

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

 response = RestClient::Request.execute(方法::发布,url: 'https://newapi.example.com/token',有效载荷:{'用户名':参数[:用户名],'密码':参数[:密码],'grant_type':'密码' },标头:{ apiCode:'93de0db8-333b-4f478-aa92-2b43cdb7aa9f'})

如果您有引号,则表示您的 username 值和 password 值是硬编码的..在这里您要检索 usernamepassword 来自您的参数.

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).

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).

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.

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(

Below is my source code

Sessions Controller

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

Sessions New View

<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 %>

User Model

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' }
    )

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天全站免登陆