在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:watson-developer-cloud/java-sdk开源软件地址:https://github.com/watson-developer-cloud/java-sdk开源编程语言:Java 99.1%开源软件介绍:Watson APIs Java SDKDeprecated buildsJava client library to use the Watson APIs. AnnouncementsTone Analyzer DeprecationAs of this major release, 10.0.1, the Tone Analyzer API has been removed in preparation for deprecation. If you wish to continue using this sdk to make calls to Tone Analyzer until its final deprecation, you will have to use a previous version. On 24 February 2022, IBM announced the deprecation of the Tone Analyzer service. The service will no longer be available as of 24 February 2023. As of 24 February 2022, you will not be able to create new instances. Existing instances will be supported until 24 February 2023. As an alternative, we encourage you to consider migrating to the Natural Language Understanding service on IBM Cloud. With Natural Language Understanding, tone analysis is done by using a pre-built classifications model, which provides an easy way to detect language tones in written text. For more information, see Migrating from Watson Tone Analyzer Customer Engagement endpoint to Natural Language Understanding. Natural Language Classifier DeprecationAs of this major release, 10.0.1, the NLC API has been removed in preparation for deprecation. If you wish to continue using this sdk to make calls to NLC until its final deprecation, you will have to use a previous version. On 9 August 2021, IBM announced the deprecation of the Natural Language Classifier service. The service will no longer be available from 8 August 2022. As of 9 September 2021, you will not be able to create new instances. Existing instances will be supported until 8 August 2022. Any instance that still exists on that date will be deleted. As an alternative, we encourage you to consider migrating to the Natural Language Understanding service on IBM Cloud that uses deep learning to extract data and insights from text such as keywords, categories, sentiment, emotion, and syntax, along with advanced multi-label text classification capabilities, to provide even richer insights for your business or industry. For more information, see Migrating to Natural Language Understanding. Updating endpoint URLs from watsonplatform.netWatson API endpoint URLs at watsonplatform.net are changing and will not work after 26 May 2021. Update your calls to use the newer endpoint URLs. For more information, see https://cloud.ibm.com/docs/watson?topic=watson-endpoint-change. Before you begin
InstallationMavenAll the services: <dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>ibm-watson</artifactId>
<version>10.0.1</version>
</dependency> Only Discovery: <dependency>
<groupId>com.ibm.watson</groupId>
<artifactId>discovery</artifactId>
<version>10.0.1</version>
</dependency> GradleAll the services: 'com.ibm.watson:ibm-watson:10.0.1' Only Assistant: 'com.ibm.watson:assistant:10.0.1' Now, you are ready to see some examples. UsageThe examples within each service assume that you already have service credentials. If not, you will have to create a service in IBM Cloud. If you are running your application in IBM Cloud (or other platforms based on Cloud Foundry), you don't need to specify the
credentials; the library will get them for you by looking at the Running in IBM CloudWhen running in IBM Cloud (or other platforms based on Cloud Foundry), the library will automatically get the credentials from AuthenticationWatson services are migrating to token-based Identity and Access Management (IAM) authentication. As of
Getting credentialsTo find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:
On this page, you should be able to see your credentials for accessing your service instance. In your code, you can use these values in the service constructor or with a method call after instantiating your service. Supplying credentialsThere are two ways to supply the credentials you found above to the SDK for authentication. Credential file (easier!)With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing it and authenticating. You can get this file by clicking the Download button for the credentials in the Manage tab of your service instance. The file downloaded will be called
As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following: Discovery service = new Discovery("2019-04-30"); And that's it! If you're using more than one service at a time in your code and get two different If you would like to configure the location/name of your credential file, you can set an environment variable called export IBM_CREDENTIALS_FILE="<path>" where ManuallyIf you'd prefer to set authentication values manually in your code, the SDK supports that as well. The way you'll do this depends on what type of credentials your service instance gives you. IAMSome services use token-based Identity and Access Management (IAM) authentication. IAM authentication uses a service API key to get an access token that is passed with the call. Access tokens are valid for approximately one hour and must be regenerated. You supply either an IAM service API key or an access token:
Supplying the IAM API key: Builder pattern approach: // letting the SDK manage the IAM token
Authenticator authenticator = new IamAuthenticator.Builder()
.apikey("<iam_api_key>")
.build();
Discovery service = new Discovery("2019-04-30", authenticator); Deprecated constructor approach: // letting the SDK manage the IAM token
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
Discovery service = new Discovery("2019-04-30", authenticator); Supplying the access token: // assuming control of managing IAM token
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
Discovery service = new Discovery("2019-04-30", authenticator); Username and passwordBuilder pattern approach: Authenticator authenticator = new BasicAuthenticator.Builder()
.username("<username>")
.password("<password>")
.build();
Discovery service = new Discovery("2019-04-30", authenticator); Deprecated constructor approach: Authenticator authenticator = new BasicAuthenticator("<username>", "<password>");
Discovery service = new Discovery("2019-04-30", authenticator); ICPAuthenticating with ICP is similar to the basic username and password method, except that you need to make sure to disable SSL verification to authenticate properly. See here for more information. Authenticator authenticator = new BasicAuthenticator("<username>", "<password>");
Discovery service = new Discovery("2019-04-30", authenticator);
HttpConfigOptions options = new HttpConfigOptions.Builder()
.disableSslVerification(true)
.build();
service.configureClient(options); Cloud Pak for DataLike IAM, you can pass in credentials to let the SDK manage an access token for you or directly supply an access token to do it yourself. Builder pattern approach: // letting the SDK manage the token
Authenticator authenticator = new CloudPakForDataAuthenticator.Builder()
.url("<CP4D token exchange base URL>")
.username("<username>")
.password("<password>")
.disableSSLVerification(true)
.headers(null)
.build();
Discovery service = new Discovery("2019-04-30", authenticator);
service.setServiceUrl("<service CP4D URL>"); Deprecated constructor approach: // letting the SDK manage the token
Authenticator authenticator = new CloudPakForDataAuthenticator(
"<CP4D token exchange base URL>",
"<username>",
"<password>",
true, // disabling SSL verification
null,
);
Discovery service = new Discovery("2019-04-30", authenticator);
service.setServiceUrl("<service CP4D URL>"); // assuming control of managing the access token
Authenticator authenticator = new BearerTokenAuthenticator("<access_token>");
Discovery service = new Discovery("2019-04-30", authenticator);
service.setServiceUrl("<service CP4D URL>"); Be sure to both disable SSL verification when authenticating and set the endpoint explicitly to the URL given in Cloud Pak for Data. Using the SDKParsing responsesNo matter which method you use to make an API request ( Here's an example of how to parse that response and get additional information beyond the response model: // listing our workspaces with an instance of the Assistant v1 service
Response<WorkspaceCollection> response = service.listWorkspaces().execute();
// pulling out the specific API method response, which we can manipulate as usual
WorkspaceCollection collection = response.getResult();
System.out.println("My workspaces: " + collection.getWorkspaces());
// grabbing headers that came back with our API response
Headers responseHeaders = response.getHeaders();
System.out.println("Response header names: " + responseHeaders.names()); Configuring the HTTP clientThe HTTP client can be configured by using the
Here's an example of setting the above: Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", 8080));
IamAuthenticator authenticator = new IamAuthenticator(apiKey);
authenticator.setProxy(proxy);
Discovery service = new Discovery("2019-04-30", authenticator);
// setting configuration options
HttpConfigOptions options = new HttpConfigOptions.Builder()
.disableSslVerification(true)
.proxy(proxy)
.loggingLevel(HttpConfigOptions.LoggingLevel.BASIC)
.build();
service.configureClient(options); Making asynchronous API callsThe basic, synchronous way to make API calls with this SDK is through the use of the // make API call
Response<ListEnvironmentsResponse> response = service.listEnvironments().execute();
// continue execution However, if you need to perform these calls in the background, there are two other methods to do this asynchronously:
|
请发表评论