Page History
...
Fetch the details of an existing user from the application. This can also be used to check whether a user already exists. Retrieve user details by querying GET /api/rpc/users/user-details-by-username/{user-name} Get a user User by usernameUsername
The following examples illustrate how to retrieve an existing user (via username) in various programming languages.
...
Create a new user by providing an array of user models to POST /api/admin/users createUserAdmin Create User Admin. Pass multiple user models to create multiple users simultaneously.
...
A user's password can generally only be updated by a user themselves, or by an administrator who has sufficient rights. Passwords are updated by calling PUT /api/admin/users/{userId}/password (https://developers.yellowfinbi.com/dev/api-docs/current/#operation/setUserPasswordAdmin ).Update a User's Password.
The payload for this REST call can consist of a simple JSON entry that includes the password:
...
The following examples update an existing user in various programming languages.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package rest.code.examples;
import java.io.IOException;
import java.util.Random;
import org.apache.hc.client5.http.fluent.Content;
import org.apache.hc.client5.http.fluent.Request;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Create a user using the Yellowfin REST API
*/
public class UpdateUsersPassword {
public static void main(String[] args) throws Exception {
String host = ""http://localhost/yellowfinHead"";
String restUsername = ""admin@yellowfin.com.au"";
String restPassword = ""test"";
String userToUpdate = ""user1@yellowfin.com.au"";
String newPassword = ""test"";
String token = generateToken(host, restUsername, restPassword);;
Integer userIpId = retrieveUserIpIdForUsername(host, token, userToUpdate);
String updatePasswordPayload =
""{"" +
"" \""password\"": \"""" + newPassword + ""\"""" +
""}"";
System.out.println(""Payload: "" + updatePasswordPayload);
Content c = Request.put(host + ""/api/admin/users/"" + userIpId + ""/password"")
.addHeader(""Authorization"", ""YELLOWFIN ts="" + System.currentTimeMillis() + "" , nonce="" + new Random().nextLong() + "", token="" + token)
.addHeader(""Accept"", ""application/vnd.yellowfin.api-v1+json"")
.addHeader(""Content-Type"", ""application/json"")
.bodyString(updatePasswordPayload, null)
.execute().returnContent();
System.out.print(c.asString());
}
public static Integer retrieveUserIpIdForUsername(String host, String token, String userName) throws IOException {
Content c = Request.get(host + ""/api/rpc/users/user-details-by-username/"" + userName)
.addHeader(""Authorization"", ""YELLOWFIN ts="" + System.currentTimeMillis() + "" , nonce="" + new Random().nextLong()+ "", token="" + token)
.addHeader(""Accept"", ""application/vnd.yellowfin.api-v1+json"")
.addHeader(""Content-Type"", ""application/json"")
.execute().returnContent();
JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
JsonElement userIpJsonAttribute = jsonObject.get(""userId"");
Integer userIpId = userIpJsonAttribute.getAsInt();
return userIpId;
}
/*
* This function generates an access token for a user that will grant them access to
* call REST API endpoints.
*/
public static String generateToken(String host, String username, String password) throws IOException {
Content c = Request.post(host + ""/api/refresh-tokens"")
.addHeader(""Authorization"", ""YELLOWFIN ts="" + System.currentTimeMillis() + "" , nonce="" + new Random().nextLong())
.addHeader(""Accept"", ""application/vnd.yellowfin.api-v1+json"")
.addHeader(""Content-Type"", ""application/json"")
.bodyString(""{ \""userName\"": \""""+ username + ""\"",\""password\"": \""""+ password + ""\""}"", null)
.execute().returnContent();
JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject();
JsonElement accessToken = jsonObject.getAsJsonObject(""_embedded"").getAsJsonObject(""accessToken"").get(""securityToken"");
if (accessToken!=null) {
System.out.println(""Access Token: "" + accessToken);
} else {
System.out.println(""Token not retrieved successfully"");
System.exit(-1);
}
return accessToken.getAsString();
}
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
namespace YellowfinAPIExamples
{
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class UpdateUsersPassword
{
static async Task Main(string[] args)
{
string host = ""http://localhost:8080/Yellowfin"";
string restUsername = ""admin@yellowfin.com.au"";
string restPassword = ""test"";
string userToUpdate = ""user1@yellowfin.com.au"";
string newPassword = ""test"";
string token = await GenerateToken(host, restUsername, restPassword);
int userIpId = await RetrieveUserIpIdForUsername(host, token, userToUpdate);
string updatePasswordPayload = JsonConvert.SerializeObject(new { password = newPassword });
Console.WriteLine(""Payload: "" + updatePasswordPayload);
long nonce = new Random().NextInt64();
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(""YELLOWFIN"", $""ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()} , nonce={nonce}, token={token}"");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(""application/vnd.yellowfin.api-v1+json""));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, $""{host}/api/admin/users/{userIpId}/password"")
{
Content = new StringContent(updatePasswordPayload, System.Text.Encoding.UTF8, ""application/json"")
};
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine(""Failed to update user password. Status code: "" + response.StatusCode);
}
}
}
static async Task<int> RetrieveUserIpIdForUsername(string host, string token, string userName)
{
long nonce = new Random().NextInt64();
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(""YELLOWFIN"", $""ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()} , nonce={nonce}, token={token}"");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(""application/vnd.yellowfin.api-v1+json""));
HttpResponseMessage response = await httpClient.GetAsync($""{host}/api/rpc/users/user-details-by-username/{userName}"");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseBody);
int userIpId = jsonObject[""userId""].Value<int>();
return userIpId;
}
else
{
throw new Exception(""Failed to retrieve user IP ID. Status code: "" + response.StatusCode);
}
}
}
static async Task<string> GenerateToken(string host, string restUsername, string restPassword)
{
using (var client = new HttpClient())
{
long nonce = new Random().NextInt64();
var request = new HttpRequestMessage(HttpMethod.Post, $""{host}/api/refresh-tokens"");
request.Headers.Add(""Authorization"", $""YELLOWFIN ts={DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, nonce={nonce}"");
request.Headers.Add(""Accept"", ""application/vnd.yellowfin.api-v1+json"");
request.Content = new StringContent(
JsonConvert.SerializeObject(new { userName = restUsername, password = restPassword }),
System.Text.Encoding.UTF8,
""application/json""
);
HttpResponseMessage response = await client.SendAsync(request);
string responseContent = await response.Content.ReadAsStringAsync();
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(responseContent);
string accessToken = jsonObject[""_embedded""][""accessToken""][""securityToken""].ToString();
if (!string.IsNullOrEmpty(accessToken))
{
Console.WriteLine(""Access Token: "" + accessToken);
}
else
{
Console.WriteLine(""Token not retrieved"");
Environment.Exit(-1);
}
return accessToken;
}
}
}
} |
Code Block | ||||
---|---|---|---|---|
| ||||