Embedding in a Java app - example


#1

Hi,

The embedding feature look nice. Currently 4 examples are given but for non Java application.
I tried to do insert the code in a java app by including the io.jsonwebtoken library?
I also used the org.json.JSONObject to build a json object.

// Fisrt, I create the Json object
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put(“dashboard”, “2”);
JSONObject jsonObject = new JSONObject();
jsonObject.put(“resource”, jsonObject1);
jsonObject.put(“params”, “”);

// Then my claims
Claims claims2 = Jwts.claims();
claims2.put(“payload”,jsonObject.toString());

//Finally the token
String token = Jwts.builder()
.setClaims(claims2)
.signWith(SignatureAlgorithm.HS256, METABASE_SECRET_KEY)
.compact();

//then, I insert my iframeUrl string.
String iframeUrl = METABASE_SITE_URL + “/embed/dashboard/” + token + “#bordered=true&titled=true”;

When I load my page, I got a metabase feedback : “Message seems corrupt or manipulated.”

Claims 2 generated by the program : {payload={“resource”:{“dashboard”:“2”},“params”:""}}
If I do a java - claims.get(“payload”); - , I get : {“resource”:{“dashboard”:“2”},“params”:""}

Everything looks ok. I do not understand why it is not working. I have check my metabase_secret_key, that the embedded dashboard is published …

Did anyone succeed to embed a dashboard in a java app ? If yes, help is welcome.

Thanks in advance,

FBS


#2

Dear all,

After searching and searching, I finally change of library going to the org.jose4j library. I could not find why it was not working but now, everything is working well. Below my code for Java embedding. Hope it can help you.

JSONObject jsonObject1 = new JSONObject();
jsonObject1.put(“dashboard”, 2);
JSONObject jsonObject = new JSONObject();
jsonObject.put(“resource”, jsonObject1);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put(“category”, “Widget”);
jsonObject.put(“params”,jsonObject2);

    JwtClaims claims = new JwtClaims();
    
    // Set Key
    Key key = null;
	try {
		key = new HmacKey(METABASE_SECRET_KEY.getBytes("UTF-8"));
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
    JsonWebSignature jws = new JsonWebSignature();
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
    jws.setKey(key);
    jws.setPayload(jsonObject.toString());
    jws.setDoKeyValidation(false); // relaxes the key length requirement
    String jwt = null;
	try {
		jwt = jws.getCompactSerialization();
	} catch (JoseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	String iframeUrl = METABASE_SITE_URL + "/embed/dashboard/" + jwt + "#bordered=true&titled=true";
             embedded1.setSource(iframeUrl);

Where embedded is my container and setsource the function to set the source.
Of course, you need to setup your METABASE_SITE_URL and METABASE_SECRET_KEY.
FBS


#3

Had the similar problem with nimbusd library, and reverted back to the org.jose4 library and I seem to be getting further with embedded setup


WIKI: SDK (covers API, embedding etc?)
#4

Spring MVC example:


#5

Hi @fbeaufil, good morning!

Could you tell us what the “imports” of this Java class are?

I’m trying to implement here.

Regards,
Lourival Oliveira