Creating a new user with credentials, then obtaining a token for that user with Doorkeeper in an API

It was the simplest of things! I was overcomplicating it by trying to POST, when in actual fact I could simply generate the DoorKeeper::AccessToken in user#create, and then return this.

Here's the code to generate the token:

access_token = Doorkeeper::AccessToken.create!(:application_id => application_id, :resource_owner_id => user_id)

I dig a bit in the doorkeeper source code, like the way that creating token using standard api way, you'd better using the following method if you are manually doing this.

find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)

for your case

access_token = Doorkeeper::AccessToken.find_or_create_for(application: application, resource_owner_id: user_id)

link to source code of doorkeeper find_or_create_for in doorkeeper


In rails we can create access token using DoorKeeper using:

Doorkeeper::AccessToken.create!(
  application_id: nil,
  resource_owner_id: user.id,
  expires_in: 2.hours,
  scopes: 'public'
)