首页 文章

无法使用smooch-java从Android应用程序发送短信

提问于
浏览
0

我尝试使用_1070460从Android应用程序发送短信,但每次都收到错误的请求 .

我的代码:

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);
            }
        }

错误日志:

io.smooch.client.ApiException: Bad Request

{"error":{"code":"bad_request","description":".items: should not have less than 1 items"}}

Docs,这是一条短信,我不需要任何 items 但错误说我需要至少一个 item

2 回答

  • 0

    似乎这是与this issue相关的生成包装器中的错误 .

    Smooch codegen fork已经更新,包装器的新版本已经released . 此版本应该可以正常工作 .

  • 0

    我已将lib更新为1.2.0但仍然出错 . 但我通过将项设置为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);
            }
        }
    

    它奏效了

相关问题