I tried to send a text message from Android app using smooch-java
but got bad request everytime.
My code:
String jwt = "";
try {
jwt = Jwts.builder()
.claim("scope", "app")
.setHeaderParam(KEY_ID, "MY_KEY_ID")
.signWith(
SignatureAlgorithm.HS256,
"MY_SECRET".getBytes("UTF-8")
)
.compact();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (!jwt.isEmpty()) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) defaultClient.getAuthentication("jwt");
apiKeyAuth.setApiKey(jwt);
apiKeyAuth.setApiKeyPrefix("Bearer");
ConversationApi apiInstance = new ConversationApi();
String userId = preferencesRepository.getUserId();
MessagePost messagePostBody = new MessagePost();
messagePostBody.setRole("appUser");
messagePostBody.setType(MessagePost.TypeEnum.TEXT);
messagePostBody.setText(message);
try {
PostMessagesResponse result = apiInstance.postMessage(userId, messagePostBody);
Log.d(TAG, "sendMessage: " + result);
} catch (ApiException e) {
Log.e(TAG, "sendMessage:", e);
}
}
Error logs:
io.smooch.client.ApiException: Bad Request
{"error":{"code":"bad_request","description":".items: should not have less than 1 items"}}
From Docs, This is a text message and I don't need any items
but the error said that I need at least one item
I have updated the lib to 1.2.0 but still got error. But I fixed it by setting items to null:
String jwt = "";
try {
jwt = Jwts.builder()
.claim("scope", "app")
.setHeaderParam(KEY_ID, "MY_KEY_ID")
.signWith(
SignatureAlgorithm.HS256,
"MY_SECRET".getBytes("UTF-8")
)
.compact();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (!jwt.isEmpty()) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) defaultClient.getAuthentication("jwt");
apiKeyAuth.setApiKey(jwt);
apiKeyAuth.setApiKeyPrefix("Bearer");
ConversationApi apiInstance = new ConversationApi();
String userId = preferencesRepository.getUserId();
MessagePost messagePostBody = new MessagePost();
messagePostBody.setRole("appUser");
messagePostBody.setType(MessagePost.TypeEnum.TEXT);
messagePostBody.setText(message);
messagePostBody.setItems(null);
try {
PostMessagesResponse result = apiInstance.postMessage(userId, messagePostBody);
Log.d(TAG, "sendMessage: " + result);
} catch (ApiException e) {
Log.e(TAG, "sendMessage:", e);
}
}
And it worked