# Programmatically create user with OAuth2 user ID

**URL:** https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920
**Category:** Support
**Created:** [July 12, 2021, 11:26pm UTC](https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920 "2021-07-12T23:26:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gnuletik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gnuletik/32/227434_2.png) [@gnuletik](https://meta.discourse.org/u/gnuletik)
#### Post date: [July 12, 2021, 11:26pm UTC](https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920/1 "2021-07-12T23:26:39Z")

</div>

Is there a way to insert a row inside the `user_associated_account` table from the REST API ?

To give you some context, I need to be able to create users on our Discourse instance and link them to our OAuth2 provider.  
When users logs in from the Discourse UI (through OAuth2 Basic Plugin), their account is linked and I can get their discourse user ID from their external UID with `GET /u/by-external/oauth2_basic/{external_uid}`.  
However, when we create users from the REST API (`POST /users.json`), I see no way to link their account to their corresponding OAuth2 account (if they don’t manually log in on Discourse).

FYI, OAuth2 basic plugin implemented the new `Auth::ManagedAuthenticator` system : [https://github.com/discourse/discourse-oauth2-basic/pull/21](https://github.com/discourse/discourse-oauth2-basic/pull/21)

What I tried :

- I looked into the API docs and did not find any clue.
- I found which API route was called to get the associated accounts from one user : `GET /u/{username}/emails.json`. But this route does not seems to support HTTP `PUT` method and does not expose the fields from the `user_associated_accounts` table (`provider_name`, `provider_uid` and `user_id`).
- I also looked into the Discourse settings if anything was related to this issue without any success.
- I checked on [meta.discourse.org](http://meta.discourse.org) and did not found a solution.

Any help is greatly appreciated! Thanks 🙂

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 13, 2021, 10:11am UTC](https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920/2 "2021-07-13T10:11:39Z")

</div>

As long as you create the user with the same email address that oauth has you don’t need to worry

---

<div class="post-metadata">

### Author: ![gnuletik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gnuletik/32/227434_2.png) [@gnuletik](https://meta.discourse.org/u/gnuletik)
#### Post date: [July 13, 2021, 10:29am UTC](https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920/3 "2021-07-13T10:29:49Z")

</div>

Thanks for your feedback !  
I’d like to not worry about it but I need to be able to get user from my provider ID ( `GET /u/by-external/oauth2_basic/{external_uid}`).  
This is not possible until the user manually logs in through UI (I want to avoid that).

---

<div class="post-metadata">

### Author: ![gnuletik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gnuletik/32/227434_2.png) [@gnuletik](https://meta.discourse.org/u/gnuletik)
#### Post date: [July 13, 2021, 1:15pm UTC](https://meta.discourse.org/t/programmatically-create-user-with-oauth2-user-id/196920/4 "2021-07-13T13:15:12Z")

</div>

I would think of exposing the `associated_accounts` like this when creating a new user :

```json
POST /users.json
{
  username: "something",
  email: "test@test.com",
  associated_accounts: [
    {
      provider_name: "oauth2_basic",
      provider_uid: "12345"
    }
  ]
}

```

This would be perfect !

Note: this is similar to the `provider` and `extern_uid` parameters of Gitlab Users API ([Users API | GitLab Docs](https://docs.gitlab.com/ee/api/users.html#user-creation))
