|
17 | 17 | const express = require('express');
|
18 | 18 |
|
19 | 19 | const app = express();
|
20 |
| -const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1'); |
21 |
| -const AuthorizationV1 = require('ibm-watson/authorization/v1'); |
22 |
| -const IamTokenManagerV1 = require('ibm-watson/iam-token-manager/v1'); |
| 20 | +const { IamTokenManager } = require('ibm-watson/auth'); |
23 | 21 |
|
24 | 22 | // Bootstrap application settings
|
25 | 23 | require('./config/express')(app);
|
26 | 24 |
|
27 |
| -// Create the token manager |
28 |
| -let tokenManager; |
29 |
| -let instanceType; |
30 |
| -const serviceUrl = process.env.SPEECH_TO_TEXT_URL || 'https://stream.watsonplatform.net/speech-to-text/api'; |
| 25 | +const serviceUrl = process.env.SPEECH_TO_TEXT_URL; |
| 26 | + |
| 27 | +const tokenManager = new IamTokenManager({ |
| 28 | + apikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY || '<iam_apikey>', |
| 29 | +}); |
31 | 30 |
|
32 |
| -if (process.env.SPEECH_TO_TEXT_IAM_APIKEY && process.env.SPEECH_TO_TEXT_IAM_APIKEY !== '') { |
33 |
| - instanceType = 'iam'; |
34 |
| - tokenManager = new IamTokenManagerV1({ |
35 |
| - iamApikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY || '<iam_apikey>', |
36 |
| - iamUrl: process.env.SPEECH_TO_TEXT_IAM_URL || 'https://iam.bluemix.net/identity/token', |
37 |
| - }); |
38 |
| -} else { |
39 |
| - instanceType = 'cf'; |
40 |
| - const speechService = new SpeechToTextV1({ |
41 |
| - username: process.env.SPEECH_TO_TEXT_USERNAME || '<username>', |
42 |
| - password: process.env.SPEECH_TO_TEXT_PASSWORD || '<password>', |
43 |
| - url: serviceUrl, |
44 |
| - }); |
45 |
| - tokenManager = new AuthorizationV1(speechService.getServiceCredentials()); |
46 |
| -} |
47 | 31 |
|
48 | 32 | app.get('/', (req, res) => res.render('index'));
|
49 | 33 |
|
50 | 34 | // Get credentials using your credentials
|
51 |
| -app.get('/api/v1/credentials', (req, res, next) => { |
52 |
| - tokenManager.getToken((err, token) => { |
53 |
| - if (err) { |
54 |
| - next(err); |
55 |
| - } else { |
56 |
| - let credentials; |
57 |
| - if (instanceType === 'iam') { |
58 |
| - credentials = { |
59 |
| - accessToken: token, |
60 |
| - serviceUrl, |
61 |
| - }; |
62 |
| - } else { |
63 |
| - credentials = { |
64 |
| - token: token.token, |
65 |
| - serviceUrl, |
66 |
| - }; |
67 |
| - } |
68 |
| - res.json(credentials); |
69 |
| - } |
70 |
| - }); |
| 35 | +app.get('/api/v1/credentials', async (req, res, next) => { |
| 36 | + try { |
| 37 | + const accessToken = await tokenManager.getToken(); |
| 38 | + res.json({ |
| 39 | + accessToken, |
| 40 | + serviceUrl, |
| 41 | + }); |
| 42 | + } catch (err) { |
| 43 | + next(err); |
| 44 | + } |
71 | 45 | });
|
72 | 46 |
|
73 | 47 | module.exports = app;
|
0 commit comments