Mise à jour d'un utilisateur
Dans cet extrait de code, vous apprendrez comment mettre à jour les détails d'un utilisateur.
Exemple
Veillez à ce que les variables suivantes soient réglées sur les valeurs souhaitées en utilisant toute méthode appropriée :
| Clé | Description |
|---|---|
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. |
Conditions préalables
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.
Rédiger le code
Ajouter ce qui suit à 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'"
}'Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Conditions préalables
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-sdkCréez un fichier nommé update-user.js et ajoutez le code suivant :
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Rédiger le code
Ajouter ce qui suit à 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();Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Conditions préalables
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.
Ajouter ce qui suit à build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Créez un fichier nommé UpdateUser et ajoutez le code suivant à la méthode main:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Rédiger le code
Ajouter ce qui suit à la méthode main du fichier UpdateUser:
val user = client.users.user(USER_ID).update {
name(USER_NEW_NAME)
displayName(USER_NEW_DISPLAY_NAME)
}Exécutez votre code
Nous pouvons utiliser le plugin Applications pour Gradle afin de simplifier l'exécution de notre application. Mettez à jour votre build.gradle avec ce qui suit :
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Exécutez la commande gradle suivante pour exécuter votre application, en remplaçant com.vonage.quickstart.kt.users par le paquet contenant UpdateUser:
Conditions préalables
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.
Ajouter ce qui suit à build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Créez un fichier nommé UpdateUser et ajoutez le code suivant à la méthode main:
VonageClient client = VonageClient.builder()
.apiKey(VONAGE_API_KEY)
.apiSecret(VONAGE_API_SECRET)
.build();Rédiger le code
Ajouter ce qui suit à la méthode main du fichier UpdateUser:
User user = client.getUsersClient().updateUser(
USER_ID, User.builder()
.name(USER_NEW_NAME)
.displayName(USER_NEW_DISPLAY_NAME)
.build()
);Exécutez votre code
Nous pouvons utiliser le plugin Applications pour Gradle afin de simplifier l'exécution de notre application. Mettez à jour votre build.gradle avec ce qui suit :
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Exécutez la commande gradle suivante pour exécuter votre application, en remplaçant com.vonage.quickstart.users par le paquet contenant UpdateUser:
Conditions préalables
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 VonageCréez un fichier nommé UpdateUser.cs et ajoutez le code suivant :
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Request;
using Vonage.Users.UpdateUser;Ajouter ce qui suit à UpdateUser.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);Rédiger le code
Ajouter ce qui suit à UpdateUser.cs:
var response = await client.UsersClient.UpdateUserAsync(UpdateUserRequest.Build()
.WithId(USER_ID)
.WithName(USER_NAME)
.WithDisplayName(USER_DISPLAY_NAME)
.Create());Conditions préalables
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-dotenvRédiger le code
Ajouter ce qui suit à 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)Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Essayez-le
Lorsque vous exécutez le code, vous mettez à jour le nom et le nom d'affichage de l'utilisateur spécifié.