Page History
...
This Java Code example uses Apache HTTP Client and GSON for handling REST calls and JSON serialization. This also includes code for fetching an access token.
Copytoclipboardcode | ||||||
---|---|---|---|---|---|---|
| ||||||
package rest.code.examples; 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; /** * Request an access token from the Yellowfin refresh-tokens REST end-point */ public class GetLoginToken { public static void main(String[] args) throws Exception { String host = "http://localhost:8080/yellowfinHead"; String restUsername = "admin@yellowfin.com.au";String restPassword = String restPassword = "test"; 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\": \""+ restUsername + "\",\"password\": \""+ restPassword + "\"}", 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"); } } } |
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; class GetLoginToken { static async Task Main(string[] args) { string host = "http://localhost:8080/yellowfinHead"; string restUsername = "admin@yellowfin.com.au"; string restPassword = "test"; using (var client = new HttpClient()) { // Generate nonce long nonce = new Random().NextInt64(); // Create HTTP request 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 }), MediaTypeHeaderValue.Parse("application/json") ); // Send request and get response HttpResponseMessage response = await client.SendAsync(request); string responseContent = await response.Content.ReadAsStringAsync(); // Parse JSON response 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"); } } } } |
...