gpt4 book ai didi

ruby-on-rails - "application_id"上的门卫 ActiveRecord::NotNullViolation(密码授予)

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:30 32 4
gpt4 key购买 nike

我正在尝试设置 Doorkeeper对于我的 API(使用密码授予流程),但每当我尝试检索访问 token 时,我的 Rails 服务器上都会出现以下错误:

ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR:  null value in column "application_id" violates not-null constraint
DETAIL: Failing row contains (1, 1, null, e24EH4dNKBNoHh7OSzzyT_7Cg4Ka52e9TB4TP-vz4aM, null, 7200, null, 2019-07-19 22:50:44.797045, , ).
: INSERT INTO "oauth_access_tokens" ("resource_owner_id", "token", "expires_in", "created_at", "scopes") VALUES ($1, $2, $3, $4, $5) RETURNING "id"):

我关注了门卫的rails getting started guide除了将关联添加到我的用户模型的最后一步——我在遇到错误后尝试这样做,但没有帮助。我目前没有使用 Devise .

我是不是配置不正确?或者也许我必须使用 Devise?我不确定 application_id 字段在哪里发挥作用,因为 doorkeeper 的文档留下了应用程序页面 blank .

我发送给 API 的内容:

grant_type: password
username: test@user.com
password: password

我的 doorkeeper.rb 初始化文件:

Doorkeeper.configure do
# Change the ORM that doorkeeper will use (needs plugins)
orm :active_record

# This block will be called to check whether the resource owner is authenticated or not.
resource_owner_authenticator { current_user || render(status: 401) }

resource_owner_from_credentials do |_routes|
user = User.find_by_email(params[:username].try(:downcase))
user if user && user.authenticate(params[:password])
end

grant_flows %w[password]
end

我的看门人迁移文件:

class CreateDoorkeeperTables < ActiveRecord::Migration[5.2]
def change
create_table :oauth_applications do |t|
t.string :name, null: false
t.string :uid, null: false
t.string :secret, null: false

# Remove `null: false` if you are planning to use grant flows
# that doesn't require redirect URI to be used during authorization
# like Client Credentials flow or Resource Owner Password.
t.text :redirect_uri, null: false
t.string :scopes, null: false, default: ''
t.boolean :confidential, null: false, default: true
t.timestamps null: false
end

add_index :oauth_applications, :uid, unique: true

create_table :oauth_access_grants do |t|
t.references :resource_owner, null: false
t.references :application, null: false
t.string :token, null: false
t.integer :expires_in, null: false
t.text :redirect_uri, null: false
t.datetime :created_at, null: false
t.datetime :revoked_at
t.string :scopes
end

add_index :oauth_access_grants, :token, unique: true
add_foreign_key(
:oauth_access_grants,
:oauth_applications,
column: :application_id
)

create_table :oauth_access_tokens do |t|
t.references :resource_owner, index: true
t.references :application, null: false

# If you use a custom token generator you may need to change this column
# from string to text, so that it accepts tokens larger than 255
# characters. More info on custom token generators in:
# https://github.com/doorkeeper-gem/doorkeeper/tree/v3.0.0.rc1#custom-access-token-generator
#
# t.text :token, null: false
t.string :token, null: false

t.string :refresh_token
t.integer :expires_in
t.datetime :revoked_at
t.datetime :created_at, null: false
t.string :scopes

# If there is a previous_refresh_token column,
# refresh tokens will be revoked after a related access token is used.
# If there is no previous_refresh_token column,
# previous tokens are revoked as soon as a new access token is created.
# Comment out this line if you'd rather have refresh tokens
# instantly revoked.
t.string :previous_refresh_token, null: false, default: ""
end

add_index :oauth_access_tokens, :token, unique: true
add_index :oauth_access_tokens, :refresh_token, unique: true
add_foreign_key(
:oauth_access_tokens,
:oauth_applications,
column: :application_id
)

# Uncomment below to ensure a valid reference to the resource owner's table
# add_foreign_key :oauth_access_grants, <model>, column: :resource_owner_id
add_foreign_key :oauth_access_tokens, <model>, column: :resource_owner_id
end
end

我的用户模型:

class User < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
# VALID_EMAIL_REGEX = (removed for this post)
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end

最佳答案

  1. 创建门卫应用。

    • 无 API 模式:只需导航至/oauth/applications/new
    • API 模式:您可以从 Rails 控制台创建应用程序:

      application = Doorkeeper::Application.create(
      名称:“应用程序名称”,
      范围:“读写”,
      secret :假的,
      redirect_uri: "urn:ietf:wg:oauth:2.0:oob"
      )

  2. 现在您可以使用applicationuid (client_id) 和secret 来请求访问 token 。 密码流curl请求示例:

    `curl --location --request POST 'http://localhost:3000/oauth/token?client_id=CLIENT_ID&client_secret=SECRET' --header 'Content-Type: application/json' --header 'Accept: application/json' --data-raw '{"username": "example@email.com", "password": "password", "grant_type": "password" }'`
  3. 其他流程(如 Authorization 流程)要求您获得一个 authorization token ,您可以使用它来请求 access token .相关wiki:https://github.com/doorkeeper-gem/doorkeeper/wiki/Authorization-Code-Flow

关于ruby-on-rails - "application_id"上的门卫 ActiveRecord::NotNullViolation(密码授予),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57120487/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com