简体   繁体   中英

IBM Watson UnAuthorized

I implemented IBM watson Assistant and it works perfectly fine on android debug. Problem comes when I build a signed apk. It always says Unauthorized. I don't think its the key because its working fine on debug mode. I need some help because the project is live. What I have tried so far is to change the key in IBM cloud and tried other keys but it raises not found exception of which i think is caused by wrong key. Im I supposed to allow something in IBM cloud for signed apk? or is there a certificate from signed apk that I have to upload in IBM cloud? Im using IBM watson Assistant v2

private Assistant watsonAssistant;
private Response<SessionResponse> watsonAssistantSession;

private void createServices() {
    watsonAssistant = new Assistant("2020-04-01", new IamAuthenticator(getString(R.string.assistant_apikey)));
    watsonAssistant.setServiceUrl(getString(R.string.assistant_url));
}

private void sendMessage(){
    Thread thread = new Thread(() -> {
                    try {
                        if (watsonAssistantSession == null) {
                            ServiceCall<SessionResponse> call = watsonAssistant.createSession(new CreateSessionOptions.Builder().assistantId(getString(R.string.normal_assistant_id)).build());
                            watsonAssistantSession = call.execute();
                        }
                        MessageInput input = new MessageInput.Builder()
                                .text(userInput)
                                .build();
                        MessageOptions options = new MessageOptions.Builder()
                                .assistantId(getString(R.string.normal_assistant_id))
                                .input(input)
                                .sessionId(watsonAssistantSession.getResult().getSessionId())
                                .build();
                        Response<MessageResponse> response = watsonAssistant.message(options).execute();
                        if (response.getResult().getOutput() != null && !response.getResult().getOutput().getGeneric().isEmpty()) {
                            List<RuntimeResponseGeneric> responses = response.getResult().getOutput().getGeneric();
                            for (RuntimeResponseGeneric r : responses) {
                                switch (r.responseType()) {
                                    case "text":
                                        aiResponse = r.text();
                                        aiConversationList.add(new AIConversation(r.text(), "ai", System.currentTimeMillis()));
                                        break;
                                    default:
                                        Log.e("Error", "Unhandled message type");
                                }
                            }
                            runOnUiThread(() -> {
                                sendConvoToServer(userInput, aiResponse);
                                txtWelcomeAI.setVisibility(View.VISIBLE);
                                aiAdapter.notifyItemInserted(aiConversationList.size() - 1);
                                userInputTxt.setEnabled(true);
                                pRecyclerView.scrollToPosition(aiConversationList.size() - 1);
                                aStatus.setText("online");

                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("IBM_EXCEPTION", e.toString());
                        aiConversationList.add(new AIConversation("Oops! Something went wrong", "ai", System.currentTimeMillis()));
                        aiAdapter.notifyItemInserted(aiConversationList.size() - 1);
                        runOnUiThread(() -> {
                            pRecyclerView.scrollToPosition(aiConversationList.size() - 1);
                            aStatus.setText("online");
                            userInputTxt.setEnabled(true);
                        });
                    }
                });
                thread.start();
}

In case anyone else will have a problem between debug and release apks like the one I had, try to check if you have done obfuscation. If so, then obfuscation is probably a problem. At least it was for me. So, either disable obfuscation from your build.gradle on app level or add some rules in proguard-rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM