Prateek Singh Chauhan
Prateek Singh Chauhan

Reputation: 33

Smooch.io postmessage api passing jwt in header param

How to pass jwt bearer token in header postal using java code of i do not want to change in smooch java code

I have tried changing in postMessageCall of conversation api with this it works fine

But i do not want to change the smooch code

    ConversationApi conversationApi = new ConversationApi();

    Action action = new Action();
    action.setType(TypeEnum.REPLY);

    action.setText("reply");


    ArrayList<Action> actionList = new ArrayList<Action>();
    actionList.add(action);

    MessageItem item1 = new MessageItem();
    item1.setActions(actionList);
    item1.setTitle("yes");

    ArrayList<MessageItem> messageItems = new ArrayList<>();
    messageItems.add(item1);

    MessagePost messagePost = new MessagePost();
    messagePost.setText("amit thak gya hai ");
    messagePost.setType(io.smooch.client.model.MessagePost.TypeEnum.TEXT);
    messagePost.setRole("appMaker");
    messagePost.setItems(null);
    messagePost.setActions(null);

    ApiClient apiClient = new ApiClient();
    Map<String, String> setJWTToken = setJWTToken(apiClient);
    // String userId = userId;
    try {
        // conversationApi.setApiClient(setJWTToken);
        PostMessagesResponse postMessage = conversationApi.postMessage(userId, messagePost);
        System.out.println("Response : " + postMessage);
    } catch (ApiException exception) {
        // TODO: prateek. Write exception handling code
        System.out.println("Exception : " + exception);
    }
}

private static Map<String, String> setJWTToken(ApiClient apiClient) {
    Map<String, String> localVarHeaderParams = new HashMap<String, String>();

    final String bearer = "Bearer some token value";
    final String[] localVarAccepts = {bearer};
    final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
    if (localVarAccept != null) {
        localVarHeaderParams.put("Accept", localVarAccept);
    }
    localVarHeaderParams.put("Authorization", bearer);
    return localVarHeaderParams;
}

}

Upvotes: 0

Views: 122

Answers (1)

Prateek Singh Chauhan
Prateek Singh Chauhan

Reputation: 33

Earlier the flow was My Class -> conversationAPI -> apiClient now i have skipped the middle layer by setting header param in My Class and flow is My Class -> apiClient

Upvotes: 1

Related Questions