Skip to content

Commit 2958c36

Browse files
Merge pull request #69 from watson-developer-cloud/access-token-query-param
provide an option for access_token
2 parents c3d8f56 + 54395ff commit 2958c36

File tree

8 files changed

+43
-23
lines changed

8 files changed

+43
-23
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ See a more advanced example at https://speech-to-text-demo.mybluemix.net/
5858
All API methods require an auth token that must be [generated server-side](https://github.com/watson-developer-cloud/node-sdk#authorization).
5959
(See https://github.com/watson-developer-cloud/speech-javascript-sdk/tree/master/examples/ for a couple of basic examples in Node.js and Python.)
6060

61+
_NOTE_: The `token` parameter only works for CF instances of services. For RC services using IAM for authentication, the `access_token` parameter must be used.
62+
6163
## [`WatsonSpeech.TextToSpeech`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_text-to-speech.html)
6264

63-
### [`.synthesize({text, token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_text-to-speech_synthesize.html) -> `<audio>`
65+
### [`.synthesize({text, token||access_token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_text-to-speech_synthesize.html) -> `<audio>`
6466

6567
Speaks the supplied text through an automatically-created `<audio>` element.
6668
Currently limited to text that can fit within a GET URL (this is particularly an issue on [Internet Explorer before Windows 10](http://stackoverflow.com/questions/32267442/url-length-limitation-of-microsoft-edge)
@@ -80,7 +82,7 @@ The `recognizeMicrophone()` and `recognizeFile()` helper methods are recommended
8082

8183
The core of the library is the [RecognizeStream] that performs the actual transcription, and a collection of other Node.js-style streams that manipulate the data in various ways. For less common use-cases, the core components may be used directly with the helper methods serving as optional templates to follow. The full library is documented at http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_speech-to-text.html
8284

83-
### [`.recognizeMicrophone({token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_speech-to-text_recognize-microphone.html) -> Stream
85+
### [`.recognizeMicrophone({token||access_token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_speech-to-text_recognize-microphone.html) -> Stream
8486

8587
Options:
8688
* `keepMicrophone`: if true, preserves the MicrophoneStream for subsequent calls, preventing additional permissions requests in Firefox
@@ -96,7 +98,7 @@ Also note that Chrome requires https (with a few exceptions for localhost and su
9698
No more data will be set after `.stop()` is called on the returned stream, but additional results may be recieved for already-sent data.
9799

98100

99-
### [`.recognizeFile({data, token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_speech-to-text_recognize-file.html) -> Stream
101+
### [`.recognizeFile({data, token||access_token})`](http://watson-developer-cloud.github.io/speech-javascript-sdk/master/module-watson-speech_speech-to-text_recognize-file.html) -> Stream
100102

101103
Can recognize and optionally attempt to play a URL, [File](https://developer.mozilla.org/en-US/docs/Web/API/File) or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
102104
(such as from an `<input type="file"/>` or from an ajax request.)

speech-to-text/get-models.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,27 @@
3737
3838
* @todo define format in @return statement
3939
* @param {Object} options
40-
* @param {String} options.token auth token
40+
* @param {String} options.token auth token for CF services
41+
* @param {String} options.access_token IAM access token for RC services
4142
* @return {Promise.<T>}
4243
*/
4344
module.exports = function getModels(options) {
44-
if (!options || !options.token) {
45-
throw new Error('Watson SpeechToText: missing required parameter: options.token');
45+
if (!options || (!options.token && !options.access_token)) {
46+
throw new Error('Watson SpeechToText: missing required auth parameter: options.token (CF) or options.access_token (RC)');
4647
}
4748
var reqOpts = {
4849
credentials: 'omit',
4950
headers: {
5051
accept: 'application/json'
5152
}
5253
};
53-
return fetch('https://stream.watsonplatform.net/speech-to-text/api/v1/models?watson-token=' + options.token, reqOpts)
54+
var url;
55+
if (options.access_token) {
56+
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/models?access_token=' + options.access_token;
57+
} else {
58+
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/models?watson-token=' + options.token;
59+
}
60+
return fetch(url, reqOpts)
5461
.then(function(response) {
5562
return response.json();
5663
})

speech-to-text/recognize-file.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ var fetch = require('nodeify-fetch'); // like regular fetch, but with an extra m
3636
* (e.g. from a file <input>, a dragdrop target, or an ajax request)
3737
*
3838
* @param {Object} options - Also passed to {MediaElementAudioStream} and to {RecognizeStream}
39-
* @param {String} options.token - Auth Token - see https://github.com/watson-developer-cloud/node-sdk#authorization
39+
* @param {String} options.token - Auth Token for CF services - see https://github.com/watson-developer-cloud/node-sdk#authorization
40+
* @param {String} options.access_token - IAM Access Token for RC services - see https://github.com/watson-developer-cloud/node-sdk#authorization
4041
* @param {Blob|FileString} options.file - String url or the raw audio data as a Blob or File instance to be transcribed (and optionally played). Playback may not with with Blob or File on mobile Safari.
4142
* @param {Boolean} [options.play=false] - If a file is set, play it locally as it's being uploaded
4243
* @param {Boolena} [options.format=true] - pipe the text through a {FormatStream} which performs light formatting. Also controls smart_formatting option unless explicitly set.
@@ -49,8 +50,8 @@ var fetch = require('nodeify-fetch'); // like regular fetch, but with an extra m
4950
*/
5051
module.exports = function recognizeFile(options) {
5152
// eslint-disable-line complexity
52-
if (!options || !options.token) {
53-
throw new Error('WatsonSpeechToText: missing required parameter: opts.token');
53+
if (!options || (!options.token && !options.access_token)) {
54+
throw new Error('WatsonSpeechToText: missing required parameter: opts.token (CF) or opts.access_token (RC)');
5455
}
5556

5657
if (options.data && !options.file) {

speech-to-text/recognize-microphone.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ var bitBucket = new Writable({
4444
* Create and return a RecognizeStream sourcing audio from the user's microphone
4545
*
4646
* @param {Object} options - Also passed to {RecognizeStream}, and {FormatStream} when applicable
47-
* @param {String} options.token - Auth Token - see https://github.com/watson-developer-cloud/node-sdk#authorization
47+
* @param {String} options.token - Auth Token for CF services - see https://github.com/watson-developer-cloud/node-sdk#authorization
48+
* @param {String} options.access_token - IAM Access Token for RC services - see https://github.com/watson-developer-cloud/node-sdk#authorization
4849
* @param {Boolean} [options.format=true] - pipe the text through a FormatStream which performs light formatting. Also controls smart_formatting option unless explicitly set.
4950
* @param {Boolean} [options.keepMicrophone=false] - keeps an internal reference to the microphone stream to reuse in subsequent calls (prevents multiple permissions dialogs in firefox)
5051
* @param {String|DOMElement} [options.outputElement] pipe the text to a [WriteableElementStream](WritableElementStream.html) targeting the specified element. Also defaults objectMode to true to enable interim results.
@@ -55,8 +56,8 @@ var bitBucket = new Writable({
5556
* @return {RecognizeStream|SpeakerStream|FormatStream|ResultStream}
5657
*/
5758
module.exports = function recognizeMicrophone(options) {
58-
if (!options || !options.token) {
59-
throw new Error('WatsonSpeechToText: missing required parameter: opts.token');
59+
if (!options || (!options.token && !options.access_token)) {
60+
throw new Error('WatsonSpeechToText: missing required parameter: opts.token (CF) or opts.access_token (RC)');
6061
}
6162

6263
// the WritableElementStream works best in objectMode

speech-to-text/recognize-stream.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var OPENING_MESSAGE_PARAMS_ALLOWED = [
3838
'speaker_labels'
3939
];
4040

41-
var QUERY_PARAMS_ALLOWED = ['customization_id', 'model', 'watson-token', 'X-Watson-Learning-Opt-Out'];
41+
var QUERY_PARAMS_ALLOWED = ['customization_id', 'model', 'watson-token', 'access_token', 'X-Watson-Learning-Opt-Out'];
4242

4343
/**
4444
* pipe()-able Node.js Duplex stream - accepts binary audio and emits text/objects in it's `data` events.
@@ -53,7 +53,8 @@ var QUERY_PARAMS_ALLOWED = ['customization_id', 'model', 'watson-token', 'X-Wats
5353
* @param {Object} options
5454
* @param {String} [options.model='en-US_BroadbandModel'] - voice model to use. Microphone streaming only supports broadband models.
5555
* @param {String} [options.url='wss://stream.watsonplatform.net/speech-to-text/api'] base URL for service
56-
* @param {String} [options.token] - Auth token
56+
* @param {String} [options.token] - Auth token for CF services
57+
* @param {String} options.access_token - IAM Access Token for RC services
5758
* @param {Object} [options.headers] - Only works in Node.js, not in browsers. Allows for custom headers to be set, including an Authorization header (preventing the need for auth tokens)
5859
* @param {String} [options.content-type='audio/wav'] - content type of audio; can be automatically determined from file header in most cases. only wav, flac, ogg/opus, and webm are supported
5960
* @param {Boolean} [options.interim_results=true] - Send back non-final previews of each "sentence" as it is being processed. These results are ignored in text mode.

text-to-speech/get-voices.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,27 @@
3838
3939
* @todo define format in @return statement
4040
* @param {Object} options
41-
* @param {String} options.token auth token
41+
* @param {String} options.token auth token for CF services
42+
* @param {String} options.access_token IAM access token for RC services
4243
* @return {Promise.<T>}
4344
*/
4445
module.exports = function getVoices(options) {
45-
if (!options || !options.token) {
46-
throw new Error('Watson TextToSpeech: missing required parameter: options.token');
46+
if (!options || (!options.token && !options.access_token)) {
47+
throw new Error('Watson TextToSpeech: missing required auth parameter: options.token (CF) or options.access_token (RC)');
4748
}
4849
var reqOpts = {
4950
credentials: 'omit',
5051
headers: {
5152
accept: 'application/json'
5253
}
5354
};
54-
return fetch('https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.token, reqOpts)
55+
var url;
56+
if (options.access_token) {
57+
url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.access_token;
58+
} else {
59+
url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.token;
60+
}
61+
return fetch(url, reqOpts)
5562
.then(function(response) {
5663
return response.json();
5764
})

text-to-speech/synthesize.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var QUERY_PARAMS_ALLOWED = ['voice', 'X-WDC-PL-OPT-OUT', 'X-Watson-Learning-Opt-
2929
* Creates and returns a HTML5 `<audio>` element
3030
*
3131
* @param {Object} options
32-
* @param {String} options.token auth token
32+
* @param {String} [options.token] - Auth token for CF services
33+
* @param {String} options.access_token - IAM Access Token for RC services
3334
* @param {String} options.text text to speak
3435
* @param {String} [options.voice=en-US_MichaelVoice] what voice to use - call getVoices() for a complete list.
3536
* @param {String} [options.customization_id] GUID of a custom voice model. Omit to use the voice with no customization.
@@ -41,8 +42,8 @@ var QUERY_PARAMS_ALLOWED = ['voice', 'X-WDC-PL-OPT-OUT', 'X-Watson-Learning-Opt-
4142
* @see module:watson-speech/text-to-speech/get-voices
4243
*/
4344
module.exports = function synthesize(options) {
44-
if (!options || !options.token) {
45-
throw new Error('Watson TextToSpeech: missing required parameter: options.token');
45+
if (!options || (!options.token && !options.access_token)) {
46+
throw new Error('Watson TextToSpeech: missing required parameter: options.token (CF) or options.access_token (RC)');
4647
}
4748
options['watson-token'] = options.token;
4849
delete options.token;

util/querystring.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
exports.stringify = function stringify(queryParams) {
1414
return Object.keys(queryParams)
1515
.map(function(key) {
16-
return key + '=' + (key === 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // the server chokes if the token is correctly url-encoded
16+
return key + '=' + (key === 'watson-token' || key === 'access_token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // the server chokes if the token is correctly url-encoded
1717
})
1818
.join('&');
1919
};

0 commit comments

Comments
 (0)