Chilkat Online Tools

SQL Server / Auth0 Management API / Update a user

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "blocked": true,
    --   "email_verified": true,
    --   "email": "",
    --   "verify_email": true,
    --   "phone_number": "",
    --   "phone_verified": true,
    --   "verify_phone_number": true,
    --   "password": "",
    --   "verify_password": true,
    --   "user_metadata": {},
    --   "app_metadata": {},
    --   "connection": "",
    --   "username": "",
    --   "client_id": ""
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'blocked', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'email_verified', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'email', ''
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'verify_email', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'phone_number', ''
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'phone_verified', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'verify_phone_number', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'password', ''
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'verify_password', 1
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'user_metadata'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'app_metadata'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'connection', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'username', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'client_id', ''

    -- Adds the "Authorization: Bearer {{auth0_token}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{auth0_token}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'PATCH', 'https://{{auth0_domain}}/api/v2/users/:id', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbRequestBody


END
GO

Curl Command

curl -X PATCH
	-H "Authorization: Bearer {{auth0_token}}"
	-H "Content-Type: application/json"
	-d '{
  "blocked": true,
  "email_verified": true,
  "email": "",
  "verify_email": true,
  "phone_number": "",
  "phone_verified": true,
  "verify_phone_number": true,
  "password": "",
  "verify_password": true,
  "user_metadata": {},
  "app_metadata": {},
  "connection": "",
  "username": "",
  "client_id": ""
}'
https://{{auth0_domain}}/api/v2/users/:id

Postman Collection Item JSON

{
  "name": "Update a user",
  "request": {
    "method": "PATCH",
    "header": [
      {
        "key": "Authorization",
        "value": "Bearer {{auth0_token}}"
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n  \"blocked\": true,\r\n  \"email_verified\": true,\r\n  \"email\": \"\",\r\n  \"verify_email\": true,\r\n  \"phone_number\": \"\",\r\n  \"phone_verified\": true,\r\n  \"verify_phone_number\": true,\r\n  \"password\": \"\",\r\n  \"verify_password\": true,\r\n  \"user_metadata\": {},\r\n  \"app_metadata\": {},\r\n  \"connection\": \"\",\r\n  \"username\": \"\",\r\n  \"client_id\": \"\"\r\n}"
    },
    "url": {
      "raw": "https://{{auth0_domain}}/api/v2/users/:id",
      "protocol": "https",
      "host": [
        "{{auth0_domain}}"
      ],
      "path": [
        "api",
        "v2",
        "users",
        ":id"
      ],
      "variable": [
        {
          "key": "id",
          "type": "any"
        }
      ]
    },
    "description": "Updates a user with the object's properties received in the request's body (the object should be a JSON object).<br/>These are the attributes that can be updated at the root level:<ul><li>blocked</li><li>email_verified</li><li>email</li><li>verify_email</li><li>password</li><li>phone_number</li><li>phone_verified</li><li>verify_password</li><li>user_metadata</li><li>app_metadata</li><li>username</li></ul>Some considerations:<ul><li>The properties of the new object will replace the old ones.</li><li>The metadata fields are an exception to this rule (<code>user_metadata</code> and <code>app_metadata</code>). These properties are merged instead of being replaced but be careful, the merge only occurs on the first level.</li><li>If you are updating <code>email_verified</code>, <code>phone_verified</code>, <code>username</code> or <code>password</code> you need to specify the <code>connection</code> property too.</li><li>If your are updating <code>email</code> or <code>phone_number</code> you need to specify the <code>connection</code> and the <code>client_id</code> properties.</ul><h5>Updating a field (non-metadata property)</h5>To mark the email address of a user as verified, the body to send should be:<pre><code>{ \"email_verified\": true }</code></pre><h5>Updating a user metadata root property</h5>Let's asume that our test user has the following <code>user_metadata</code>:<pre><code>{ \"user_metadata\" : { \"profileCode\": 1479 } }</code></pre>To add the field <code>addresses</code> the body to send should be:<pre><code>{ \"user_metadata\" : { \"addresses\": {\"work_address\": \"100 Industrial Way\"} }}</code></pre>The modified object ends up with the following <code>user_metadata</code> property:<pre><code>{\n  \"user_metadata\": {\n    \"profileCode\": 1479,\n    \"addresses\": { \"work_address\": \"100 Industrial Way\" }\n  }\n}</code></pre><h5>Updating an inner user metadata property</h5>Starting from previous user metadata, if we want to add the field <code>\"home_address\": \"742 Evergreen Terrace\"</code> in <code>addresses</code> we should send the whole addresses object (since this object is on the first level, it will be merged in, but it's own properties won't). The body to send should be:\n<pre><code>{\n  \"user_metadata\": {\n    \"addresses\": {\n      \"work_address\": \"100 Industrial Way\",\n      \"home_address\": \"742 Evergreen Terrace\"\n    }\n  }\n}</code></pre>The modified object ends up with the following <code>user_metadata</code> property:<pre><code>{\n  \"user_metadata\": {\n    \"profileCode\": 1479,\n    \"addresses\": {\n      \"work_address\": \"100 Industrial Way\",\n      \"home_address\": \"742 Evergreen Terrace\"\n    }\n  }\n}</code></pre>"
  },
  "response": [
  ]
}