Java源码示例:com.ibm.cloud.sdk.core.security.Authenticator
示例1
private Authenticator configAuth(WatsonConfigurationProperties config, String serviceName) {
String iamApiKey = config.getIamApiKey();
if (iamApiKey != null) {
return new IamAuthenticator(iamApiKey);
}
String username = config.getUsername();
String password = config.getPassword();
if (username != null && password != null) {
return new BasicAuthenticator(username, password);
}
String apiKey = config.getApiKey();
if (apiKey != null) {
return new WatsonApiKeyAuthenticator(apiKey);
}
// If we can't find the right properties, we'll return what we get from the auth config factory, which will
// pull from things like VCAP_SERVICES.
return ConfigBasedAuthenticatorFactory.getAuthenticator(serviceName);
}
示例2
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("natural_language_understanding.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new NaturalLanguageUnderstanding("2019-07-12", authenticator);
service.setDefaultHeaders(getDefaultHeaders());
service.setServiceUrl(getProperty("natural_language_understanding.url"));
}
示例3
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
*
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("personality_insights.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new PersonalityInsights(VERSION, authenticator);
service.setServiceUrl(getProperty("personality_insights.url"));
service.setDefaultHeaders(getDefaultHeaders());
}
示例4
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String iamApiKey = getProperty("visual_recognition.apikey");
Assume.assumeFalse(
"config.properties doesn't have valid credentials.",
(iamApiKey == null) || iamApiKey.equals("API_KEY"));
Authenticator authenticator = new IamAuthenticator(iamApiKey);
service = new VisualRecognition(VERSION, authenticator);
service.setDefaultHeaders(getDefaultHeaders());
}
示例5
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String iamApiKey = getProperty("visual_recognition.apikey");
Assume.assumeFalse(
"config.properties doesn't have valid credentials.",
(iamApiKey == null) || iamApiKey.equals("API_KEY"));
String url = getProperty("visual_recognition.url");
classifierId = getProperty("visual_recognition.classifier_id");
Authenticator authenticator = new IamAuthenticator(iamApiKey);
service = new VisualRecognition(VERSION, authenticator);
service.setDefaultHeaders(getDefaultHeaders());
service.setServiceUrl(url);
}
示例6
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("assistant.apikey");
workspaceId = getProperty("assistant.workspace_id");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new Assistant("2019-02-28", authenticator);
service.setServiceUrl(getProperty("assistant.url"));
service.setDefaultHeaders(getDefaultHeaders());
}
示例7
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("assistant.apikey");
assistantId = getProperty("assistant.assistant_id");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new Assistant("2019-02-28", authenticator);
service.setServiceUrl(getProperty("assistant.url"));
service.setDefaultHeaders(getDefaultHeaders());
}
示例8
public static void main(String[] args) {
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
PersonalityInsights service = new PersonalityInsights("2017-10-13", authenticator);
String text =
"Call me Ishmael. Some years ago-never mind how long "
+ "precisely-having little or no money in my purse, and nothing "
+ "particular to interest me on shore, I thought I would sail about "
+ "a little and see the watery part of the world. It is a way "
+ "I have of driving off the spleen and regulating the circulation. "
+ "Whenever I find myself growing grim about the mouth; whenever it "
+ "is a damp, drizzly November in my soul; whenever I find myself "
+ "involuntarily pausing before coffin warehouses, and bringing up "
+ "the rear of every funeral I meet; and especially whenever my "
+ "hypos get such an upper hand of me, that it requires a strong "
+ "moral principle to prevent me from deliberately stepping into "
+ "the street, and methodically knocking people's hats off-then, "
+ "I account it high time to get to sea as soon as I can.";
ProfileOptions options = new ProfileOptions.Builder().text(text).build();
Profile profile = service.profile(options).execute().getResult();
System.out.println(profile);
}
示例9
public static void main(String[] args) {
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
ToneAnalyzer service = new ToneAnalyzer("2017-09-21", authenticator);
String[] texts = {
"My charger isn't working.",
"Thanks for reaching out. Can you give me some more detail about the issue?",
"I put my charger in my tablet to charge it up last night and it keeps saying it isn't"
+ " charging. The charging icon comes on, but it stays on even when I take the charger out. "
+ "Which is ridiculous, it's brand new.",
"I'm sorry you're having issues with charging. What kind of charger are you using?"
};
List<Utterance> utterances = new ArrayList<>();
for (int i = 0; i < texts.length; i++) {
Utterance utterance = new Utterance.Builder().text(texts[i]).build();
utterances.add(utterance);
}
ToneChatOptions toneChatOptions = new ToneChatOptions.Builder().utterances(utterances).build();
// Call the service
UtteranceAnalyses utterancesTone = service.toneChat(toneChatOptions).execute().getResult();
System.out.println(utterancesTone);
}
示例10
public static void main(String[] args) {
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
ToneAnalyzer service = new ToneAnalyzer("2017-09-21", authenticator);
String text =
"I know the times are difficult! Our sales have been "
+ "disappointing for the past three quarters for our data analytics "
+ "product suite. We have a competitive data analytics product "
+ "suite in the industry. But we need to do our job selling it! "
+ "We need to acknowledge and fix our sales challenges. "
+ "We can’t blame the economy for our lack of execution! "
+ "We are missing critical sales opportunities. "
+ "Our product is in no way inferior to the competitor products. "
+ "Our clients are hungry for analytical tools to improve their "
+ "business outcomes. Economy has nothing to do with it.";
// Call the service and get the tone
ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
ToneAnalysis tone = service.tone(toneOptions).execute().getResult();
System.out.println(tone);
}
示例11
/** This only works on a Cloud Pak for Data instance, so ignoring to just run manually. */
@Test
@Ignore
public void testQueryWithSpellingSuggestions() {
Authenticator authenticator = new BearerTokenAuthenticator(""); // fill in
Discovery service = new Discovery("2019-10-03", authenticator);
service.setServiceUrl("");
HttpConfigOptions configOptions =
new HttpConfigOptions.Builder().disableSslVerification(true).build();
service.configureClient(configOptions);
QueryOptions options =
new QueryOptions.Builder()
.naturalLanguageQuery("cluod")
.spellingSuggestions(true)
.environmentId("") // fill in
.collectionId("") // fill in
.build();
QueryResponse response = service.query(options).execute().getResult();
System.out.println(response);
}
示例12
/** This only works on a Cloud Pak for Data instance, so ignoring to just run manually. */
@Test
@Ignore
public void testGetAutocompletion() {
Authenticator authenticator = new BearerTokenAuthenticator(""); // fill in
Discovery service = new Discovery("2019-10-03", authenticator);
service.setServiceUrl("");
HttpConfigOptions configOptions =
new HttpConfigOptions.Builder().disableSslVerification(true).build();
service.configureClient(configOptions);
GetAutocompletionOptions options =
new GetAutocompletionOptions.Builder()
.environmentId("") // fill in
.collectionId("") // fill in
.prefix("Ba")
.count(10L)
.build();
Completions response = service.getAutocompletion(options).execute().getResult();
System.out.println(response);
}
示例13
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
*
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String bearerToken = getProperty("discovery_v2.bearer_token");
Assume.assumeFalse("config.properties doesn't have valid credentials.", (bearerToken == null));
Authenticator authenticator = new BearerTokenAuthenticator(bearerToken);
service = new Discovery(VERSION, authenticator);
service.setDefaultHeaders(getDefaultHeaders());
service.setServiceUrl(getProperty("discovery_v2.url"));
HttpConfigOptions configOptions =
new HttpConfigOptions.Builder().disableSslVerification(true).build();
service.configureClient(configOptions);
}
示例14
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.developercloud.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String iamApiKey = getProperty("language_translator.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", (iamApiKey == null));
Authenticator authenticator = new IamAuthenticator(iamApiKey);
service = new LanguageTranslator("2018-05-01", authenticator);
service.setServiceUrl(getProperty("language_translator.url"));
// issue currently where document translation fails with learning opt-out
Map<String, String> headers = new HashMap<>();
headers.put(WatsonHttpHeaders.X_WATSON_TEST, "1");
service.setDefaultHeaders(headers);
}
示例15
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
*
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
this.customizationId = getProperty("speech_to_text.customization_id");
this.acousticCustomizationId = getProperty("speech_to_text.acoustic_customization_id");
String apiKey = getProperty("speech_to_text.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new SpeechToText(authenticator);
service.setServiceUrl(getProperty("speech_to_text.url"));
service.setDefaultHeaders(getDefaultHeaders());
}
示例16
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("natural_language_classifier.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new NaturalLanguageClassifier(authenticator);
service.setDefaultHeaders(getDefaultHeaders());
service.setServiceUrl(getProperty("natural_language_classifier.url"));
preCreatedClassifierId = getProperty("natural_language_classifier.classifier_id");
}
示例17
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.common.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String apiKey = getProperty("text_to_speech.apikey");
Assume.assumeFalse("config.properties doesn't have valid credentials.", apiKey == null);
Authenticator authenticator = new IamAuthenticator(apiKey);
service = new TextToSpeech(authenticator);
service.setServiceUrl(getProperty("text_to_speech.url"));
service.setDefaultHeaders(getDefaultHeaders());
voiceName = getProperty("text_to_speech.voice_name");
byteArrayOutputStream = new ByteArrayOutputStream();
returnedTimings = new ArrayList<>();
returnedMarks = new ArrayList<>();
}
示例18
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonAssistantConfigurationProperties.PREFIX)
public Assistant assistant() {
Authenticator authConfig = configAuth(assistantConfig, "assistant");
Assistant service = new Assistant(assistantConfig.getVersionDate(), authConfig);
configUrl(service, assistantConfig);
return service;
}
示例19
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonCompareComplyConfigurationProperties.PREFIX)
public CompareComply compareComply() {
Authenticator authConfig = configAuth(compareComplyConfig, "compare_comply");
CompareComply service = new CompareComply(compareComplyConfig.getVersionDate(), authConfig);
configUrl(service, compareComplyConfig);
return service;
}
示例20
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonDiscoveryConfigurationProperties.PREFIX)
public Discovery discovery() {
Authenticator authConfig = configAuth(discoveryConfig, "discovery");
Discovery service = new Discovery(discoveryConfig.getVersionDate(), authConfig);
configUrl(service, discoveryConfig);
return service;
}
示例21
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonLanguageTranslatorConfigurationProperties.PREFIX)
public LanguageTranslator languageTranslator() {
Authenticator authConfig = configAuth(ltConfig, "language_translator");
LanguageTranslator service = new LanguageTranslator(ltConfig.getVersionDate(), authConfig);
configUrl(service, ltConfig);
return service;
}
示例22
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonNaturalLanguageClassifierConfigurationProperties.PREFIX)
public NaturalLanguageClassifier naturalLanguageClassifier() {
Authenticator authConfig = configAuth(nlcConfig, "natural_language_classifier");
NaturalLanguageClassifier service = new NaturalLanguageClassifier(authConfig);
configUrl(service, nlcConfig);
return service;
}
示例23
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonNaturalLanguageUnderstandingConfigurationProperties.PREFIX)
public NaturalLanguageUnderstanding naturalLanguageUnderstanding() {
Authenticator authConfig = configAuth(nluConfig, "natural_language_understanding");
NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(nluConfig.getVersionDate(), authConfig);
configUrl(service, nluConfig);
return service;
}
示例24
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonPersonalityInsightsConfigurationProperties.PREFIX)
public PersonalityInsights personalityInsights() {
Authenticator authConfig = configAuth(piConfig, "personality_insights");
PersonalityInsights service = new PersonalityInsights(piConfig.getVersionDate(), authConfig);
configUrl(service, piConfig);
return service;
}
示例25
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonSpeechToTextConfigurationProperties.PREFIX)
public SpeechToText speechToText() {
Authenticator authConfig = configAuth(sttConfig, "speech_to_text");
SpeechToText service = new SpeechToText(authConfig);
configUrl(service, sttConfig);
return service;
}
示例26
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonTextToSpeechConfigurationProperties.PREFIX)
public TextToSpeech textToSpeech() {
Authenticator authConfig = configAuth(ttsConfig, "text_to_speech");
TextToSpeech service = new TextToSpeech(authConfig);
configUrl(service, ttsConfig);
return service;
}
示例27
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonToneAnalyzerConfigurationProperties.PREFIX)
public ToneAnalyzer toneAnalyzer() {
Authenticator authConfig = configAuth(taConfig, "tone_analyzer");
ToneAnalyzer service = new ToneAnalyzer(taConfig.getVersionDate(), authConfig);
configUrl(service, taConfig);
return service;
}
示例28
@Bean
@ConditionalOnMissingBean
@ConditionalOnWatsonServiceProperties(prefix = WatsonVisualRecognitionConfigurationProperties.PREFIX)
public VisualRecognition visualRecognition() {
Authenticator authConfig = configAuth(vrConfig, "visual_recognition");
VisualRecognition service = new VisualRecognition(vrConfig.getVersionDate(), authConfig);
configUrl(service, vrConfig);
return service;
}
示例29
@Test
public void textToSpeechBeanConfig() {
TextToSpeech textToSpeech = (TextToSpeech) applicationContext.getBean("textToSpeech");
assertNotNull(textToSpeech);
assertEquals(url, textToSpeech.getServiceUrl());
// Verify the credentials
assertEquals(Authenticator.AUTHTYPE_BASIC, textToSpeech.getAuthenticator().authenticationType());
BasicAuthenticator authenticator = (BasicAuthenticator) textToSpeech.getAuthenticator();
assertEquals(username, authenticator.getUsername());
assertEquals(password, authenticator.getPassword());
}
示例30
@Test
public void naturalLanguageClassifierBeanConfig() {
NaturalLanguageClassifier naturalLanguageClassifier = (NaturalLanguageClassifier) applicationContext
.getBean("naturalLanguageClassifier");
assertNotNull(naturalLanguageClassifier);
assertEquals(url, naturalLanguageClassifier.getServiceUrl());
// Verify the credentials
assertEquals(Authenticator.AUTHTYPE_BASIC, naturalLanguageClassifier.getAuthenticator().authenticationType());
BasicAuthenticator authenticator = (BasicAuthenticator) naturalLanguageClassifier.getAuthenticator();
assertEquals(username, authenticator.getUsername());
assertEquals(password, authenticator.getPassword());
}