简体   繁体   中英

How to retrieve token with IamAuthenticator IBM watson assistant v1

I try to connect to my assistant with iamAuthenticator method, it works fine but how can i get a token back back to store and reuse for another call to add new Intent or Entity

in a single methode it's fine i can can authenticate with iamAuthenticator and add Intent but i like to do that in to methods one for connecting and grab token the other one to add intent using the tocken i had back in the connect method here is code i tried and i works fine in the same method:

@PostMapping("/addIntent") // public ResponseEntity<?> void addIntent(@Valid @RequestBody IBMCredentials credentials ) throws IOException {

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/x-www-form-urlencoded");

    Authenticator authenticator = new IamAuthenticator.Builder()
            .apikey(credentials.getApiKey())
           .url("https://iam.cloud.ibm.com/identity/token")
            .disableSSLVerification(true)
            .headers(headers)
            .build();

    Assistant assistant = new Assistant(LocalDate.now().toString(), authenticator);
    assistant.setServiceUrl(credentials.getServiceUri());





    CreateIntentOptions options = new CreateIntentOptions.Builder(credentials.getWorkspaceId(), "my intent test")
            //.examples(examples)
            .build();
   
    assistant.createIntent(options).execute().getResult();
    
   

   

You probably have seen this example for Watson Assistant that comes with the Java SDK for the IBM Watson services.

To work with a service, you initialize the IAM Authenticator with the API key. Thereafter, you create the service client with the IAM Authenticator instance. Then, you reuse that service client for all calls. The access token is kept fresh by the authenticator. There should be no need by you / your code to manage it.

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