New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not send text message from android app using smooch-java #1

Closed
ghost opened this Issue May 12, 2017 · 2 comments

Comments

Projects
None yet
1 participant
@ghost

ghost commented May 12, 2017

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

@jugarrit

This comment has been minimized.

Member

jugarrit commented May 12, 2017

@jugarrit jugarrit closed this May 12, 2017

@ghost

This comment has been minimized.

ghost commented May 15, 2017

@jugarrit I still got error with the new version, 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);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment