【问题标题】:IBM Watson tokens expire in one hour. Code to refresh the tokens?IBM Watson 令牌在一小时后到期。刷新令牌的代码?
【发布时间】:2017-08-09 16:43:00
【问题描述】:

我正在设置 IBM Watson Speech-to-Text。这需要访问令牌documented here

“令牌的生存时间 (TTL) 为一小时,之后您将无法再使用它们与服务建立连接。已使用令牌建立的现有连接不受超时影响。尝试通过过期或无效的令牌会从 DataPower 引发 HTTP 401 Unauthorized 状态代码。您的应用程序代码需要准备好刷新令牌以响应此返回代码。"

我在文档页面上没有看到刷新令牌的应用程序代码示例。

我的代码是否应该为每个下载 JavaScript 应用程序的用户生成一个新令牌?或者服务器应该每小时请求一个新令牌,并在一个小时内为所有用户提供相同的令牌?

获取token的shell命令是:

curl -X GET --user 用户名:密码 \ --输出令牌\ "https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

看起来我可以从我的 JavaScript 应用程序发送一个 HTTP 请求并取回一个令牌。我是否应该将令牌作为文件请求,然后让我的应用从文件中获取令牌?

【问题讨论】:

  • 这个问题你有没有得到满意的答案?我不明白为什么他们有这种双重系统,即获得一个可以存在一个小时的令牌,然后是一个永久的令牌。为什么不将第一个永久化,或者每小时获取一个新的,或者获取第一个然后立即将其刷新为永久化?

标签: ibm-watson


【解决方案1】:

enter code here有效的方法是使用 Cloud Functions for Firebase(Google Cloud Functions 的一部分),在用户登录时触发,运行 Node 发送 HTTP 请求以获取令牌,然后将结果写入 AngularJS 值服务。

此云功能有效:

// Node modules
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request'); // node module to send HTTP requests
const fs = require('fs');

admin.initializeApp(functions.config().firebase);

exports.getWatsonToken = functions.database.ref('userLoginEvent').onUpdate(event => { // authentication trigger when user logs in

  var username = 'groucho',
      password = 'swordfish',
      url = 'https://' + username + ':' + password + '@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api';

  request({url: url}, function (error, response, body) {

    var tokenService = "app.value('watsonToken','" + body + "');";

    fs.writeFile('../public/javascript/services/watsonTokenValue.js', tokenService, (err) => {
      if (err) throw err;
        console.log('The file has been saved!');
    }); // close fs.writeFile

  }); // close request

}); // close getWatsonToken

在控制器中:

 firebase.auth().onAuthStateChanged(function(user) { // this runs on login
    if (user) { // user is signed in
      console.log("User signed in!");
      $scope.authData = user;
      firebase.database().ref('userLoginEvent').update({'user': user.uid}); // update Firebase database to trigger Cloud Function to get a new IBM Watson token
    } // end if user is signed in
    else { // User is signed out
      console.log("User signed out.");
    }
  }); // end onAuthStateChanged

遍历Cloud Function,它注入了四个Node模块,包括request用于发送HTTP请求,fs用于将结果写入文件。然后将触发器设置为更新到 Firebase 数据库(我从控制台创建)中的位置 userLoginEvent。接下来,HTTP 请求发出。响应(令牌)称为body。 app.value('watsonToken','" + body + "');" 是包装令牌的 Angular 值服务。然后fs 将所有这些写入我项目中的某个位置。

在 AngularJS 控制器中,onAuthStateChanged 在用户登录时触发。然后将 user.uid 更新为 Firebase 数据库中的位置 userLoginEvent,Cloud Function 触发,HTTP 请求发出,然后响应被写入 Angular 服务。

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2019-03-06
    • 2019-05-31
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2019-04-07
    相关资源
    最近更新 更多