User password from api

Hello friends,

I was trying to learn how to change password user. Then I have made the following script:

curl PUT ‘http://localhost:8180/auth/admin/realms/Testkeycloak/users/b8f6beac-e
cdf-441e-959c-4c9498ecb007/reset-password’
–header ‘Accept: application/json’
–header “Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA
6ICIyTGltOFR6ZkYzenJoSnphdFM2SzZKMFEzTFlBWHp6Q19HSzZTZjhZNU0wIn0.eyJleHAiOjE2Mz
Q2OTM4OTEsImlhdCI6MTYzNDY5MzgzMSwianRpIjoiMTMzNWNmMTktZDI2MC00MjgxLTljNDUtMWEwO
WNmMjg1MTBiIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MTgwL2F1dGgvcmVhbG1zL21hc3RlciIs
InN1YiI6ImQzOTI2NDBlLWE0ZmItNDkzMC04ZjZlLWI2MjAzZGZiN2YyNSIsInR5cCI6IkJlYXJlciI
sImF6cCI6ImFkbWluLWNsaSIsInNlc3Npb25fc3RhdGUiOiIyMWM4ODJhYy05NmJiLTQyNzgtYmJhZS
0xMDcwNDMzOThkNDMiLCJhY3IiOiIxIiwic2NvcGUiOiJlbWFpbCBwcm9maWxlIiwiZW1haWxfdmVya
WZpZWQiOmZhbHNlLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiJ9.FpXtS48qIEfZQjOtI7qc21x
aM_6VtIFbfBrlErwnPCf20ZqPSS_evUCNtwklbvG594Mi5FUersI38ypMD9W4jWP3-DTb8OGhw0ogOq
YAPbx8CMsQ1HtvpBRGzQnji_Ynm9ERRXdhBy3_rWLnFQFpGq-G_4JqoHGesUMDffzsa8vIvndCSbwFb
uTFdN4Am_zaDVoOlGw9HPFys_3Lj1a8NbCDXBw95KRnW4zl6Eh04P5c74YxE0Ewy9C8G9kdLYtx8Rxv
jMpsVdwwjCaou-aJAI-zt8zlrLff435qy7EBuVXYya8vWuewcZzb-tidb9hKENuOHobzScSr7k26CzL
OWA”
–header ‘Content-Type: application/json’
–data-raw ‘{
“type”: “password”,
“temporary”: false,
“value”: “qaz1234”
}’

but I have got:
curl: (6) Could not resolve host: PUT
{“error”:“HTTP 401 Unauthorized”}

My keycloak started with -Dkeycloak.profile.feature.account_api=enabled

I have read

Thank you

Seems that your bearer token is no longer valid, it is very short lived. It might be easier to use a nodejs script to do this:

    await adminClient.users.resetPassword({
      id: userId,
      credential: {
        temporary: false,
        type: 'password',
        value: 'qaz1234',
      },
    });

I think you might also have a problem with your curl command. The correct way to specify an HTTP method is with the -X flag. E.g.

curl -X PUT ‘http://localhost:8180/auth/admin/realms/Testkeycloak/users/b8f6beac-ecdf-441e-959c-4c9498ecb007/reset-password’ \
...

I think that’s why you are getting curl: (6) Could not resolve host: PUT

Hello friends,

I fixed 401 error building my script:

KEYCLOAK=http://localhost:8180
REALM=“master”
GRANT_TYPE=“password”
CLIENT=“admin-cli”
#CLIENT_SECRET=“b38eae9d-d5ef-4a98-b1e6-6b5084b09d91”
USER=“admin”
USER_PASSWORD=“Qazx1234”

echo “Keycloak host : $KEYCLOAK”
echo “Token URL : ${KEYCLOAK}/auth/realms/${REALM}/protocol/openid-connect/token”

#Get Token
#POST_BODY=“grant_type=${GRANT_TYPE}&client_id=${CLIENT}&client_secret=${CLIENT_SECRET}&username=${USER}&password=${USER_PASSWORD}”
POST_BODY=“grant_type=${GRANT_TYPE}&client_id=${CLIENT}&username=${USER}&password=${USER_PASSWORD}”

#echo POST_BODY=${POST_BODY}

RESPONSE=$(curl -k
-d ${POST_BODY}
-H “Content-Type: application/x-www-form-urlencoded”
${KEYCLOAK}/auth/realms/${REALM}/protocol/openid-connect/token)

#echo “RESPONSE”=${RESPONSE}
ACCESS_TOKEN=$(echo ${RESPONSE} | jq -r .access_token)

echo $ACCESS_TOKEN

curl PUT ‘http://localhost:8180/auth/admin/realms/Testkeycloak/users/b8f6beac-ecdf-441e-959c-4c9498ecb007/reset-password
–header ‘Accept: application/json’
–header “Authorization: Bearer ${ACCESS_TOKEN}”
–header ‘Content-Type: application/json’
–data-raw ‘{
“type”: “password”,
“temporary”: false,
“value”: “qaz1234”
}’
~

As you can see my token is from admin user and master realm. And PUT url contains “auth/admin”, I tried without “admin” too.

Now I got {“error”:“RESTEASY003650: No resource method found for POST, return 405 with Allow header”}

Finally is possible to change the password with a curl command ? or it is possible only with api ? User Password Change Programatically - #9 by gmesml

I don’t think you’re actually using a PUT. See your request and my previous message. It needs to be -X PUT

Thank you now it works fine.