Actualizar un usuario

En este fragmento de código aprenderás a actualizar los datos de un usuario.

Ejemplo

Asegúrate de que las siguientes variables estén configuradas con los valores que necesites, utilizando cualquier método que te resulte más cómodo:

ClaveDescripción
USER_ID

The unique ID of the User.

USER_NEW_NAME

The new name of the User.

USER_NEW_DISPLAY_NAME

The new display name of the User.

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.

Escriba el código

Añada lo siguiente a update-user.sh:

curl -X "PATCH" "https://api.nexmo.com/v1/users/$USER_ID" \
     -H 'Authorization: Bearer '$JWT\
     -H 'Content-Type: application/json' \
     -d $'{
  "name": "'$USER_NAME'",
  "display_name": "'$USER_DISPLAY_NAME'"
}'

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

bash update-user.sh

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.
npm install @vonage/server-sdk

Crea un archivo llamado update-user.js y añade el siguiente código:

const { Vonage } = require('@vonage/server-sdk');

const vonage = new Vonage({
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_PRIVATE_KEY,
});

Ver fuente completa

Escriba el código

Añada lo siguiente a update-user.js:

const run = async () => {
  // Load in all the user details to prevent overwriting
  const user = vonage.users.getUser(
    USER_ID,
  );

  user.name = USER_NAME;
  user.displayName = USER_DISPLAY_NAME;

  try {
    await vonage.users.updateUser(user);
    console.log(user);
  } catch (error) {
    console.error(error);
  }
};

run();

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

node update-user.js

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.

Añada lo siguiente a build.gradle:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'

Crea un archivo llamado UpdateUser y añade el siguiente código al método main:

val client = Vonage {
    applicationId(VONAGE_APPLICATION_ID)
    privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}

Ver fuente completa

Escriba el código

Añada lo siguiente al método main del archivo UpdateUser:

val user = client.users.user(USER_ID).update {
    name(USER_NEW_NAME)
    displayName(USER_NEW_DISPLAY_NAME)
}

Ver fuente completa

Ejecute su código

Podemos utilizar el plugin aplicación para Gradle para simplificar la ejecución de nuestra aplicación. Actualiza tu build.gradle con lo siguiente:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Ejecute el siguiente comando gradle para ejecutar su aplicación, sustituyendo com.vonage.quickstart.kt.users por el paquete que contiene UpdateUser:

gradle run -Pmain=com.vonage.quickstart.kt.users.UpdateUser

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.

Añada lo siguiente a build.gradle:

implementation 'com.vonage:server-sdk:9.3.1'

Crea un archivo llamado UpdateUser y añade el siguiente código al método main:

VonageClient client = VonageClient.builder()
        .apiKey(VONAGE_API_KEY)
        .apiSecret(VONAGE_API_SECRET)
        .build();

Ver fuente completa

Escriba el código

Añada lo siguiente al método main del archivo UpdateUser:

User user = client.getUsersClient().updateUser(
    USER_ID, User.builder()
        .name(USER_NEW_NAME)
        .displayName(USER_NEW_DISPLAY_NAME)
        .build()
);

Ver fuente completa

Ejecute su código

Podemos utilizar el plugin aplicación para Gradle para simplificar la ejecución de nuestra aplicación. Actualiza tu build.gradle con lo siguiente:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Ejecute el siguiente comando gradle para ejecutar su aplicación, sustituyendo com.vonage.quickstart.users por el paquete que contiene UpdateUser:

gradle run -Pmain=com.vonage.quickstart.users.UpdateUser

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.
Install-Package Vonage

Crea un archivo llamado UpdateUser.cs y añade el siguiente código:

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Request;
using Vonage.Users.UpdateUser;

Ver fuente completa

Añada lo siguiente a UpdateUser.cs:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);

Ver fuente completa

Escriba el código

Añada lo siguiente a UpdateUser.cs:

var response = await client.UsersClient.UpdateUserAsync(UpdateUserRequest.Build()
    .WithId(USER_ID)
    .WithName(USER_NAME)
    .WithDisplayName(USER_DISPLAY_NAME)
    .Create());

Ver fuente completa

Requisitos previos

You will need to use an existing Application and have a User in order to be able to update a User. See the Create Conversation code snippet for information on how to create an Application. See also the Create User code snippet on how to create a User.
pip install vonage python-dotenv

Escriba el código

Añada lo siguiente a update-user.py:

from vonage import Auth, Vonage
from vonage_users import Channels, PstnChannel, SmsChannel, User

client = Vonage(
    Auth(
        application_id=VONAGE_APPLICATION_ID,
        private_key=VONAGE_PRIVATE_KEY,
    )
)

user_params = User(
    name=USER_NAME,
    display_name=USER_DISPLAY_NAME,
    channels=Channels(
        sms=[SmsChannel(number='1234567890')], pstn=[PstnChannel(number=123456)]
    ),
)
user: User = client.users.update_user(id=USER_ID, params=user_params)

print(user)

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

python users/update-user.py

Pruébalo

Cuando ejecute el código actualizará el nombre y el nombre para mostrar del Usuario especificado.