1

很抱歉,我已经观看了Jennifer Person 的所有七个视频,阅读了文档,并完成了教程,但我仍然不知道如何编写我的函数。我正在尝试编写一个获取 IBM Watson Speech-to-text 令牌的函数,该令牌是通过以下 CURL 脚本获得的:

curl -X GET --user username:password --output token
"https://stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"

即,向 URL 发送 HTTP GET 请求,提供我的用户名和密码,然后将输出写入文件/javascript/services/token

这是我对功能的猜测。身份验证触发器包装 Nodejs HTTP GET 请求和 NodeJs 文件 save fs.writefile

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.getWatsonToken = functions.auth.user().onCreate(event => { // authentication trigger

  var https = require('https');  // Nodejs http.request

  var options = {
    host: 'stream.watsonplatform.net',
    path: '/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api',
    username: groucho,
    password: swordfish
  };

  callback = function(response) {
    response.on('end', function () {
      console.log(response);
      fs.writeFile("/javascript/services/token", response);
    });
  }

  http.request(options, callback).end();

});
4

1 回答 1

2

此功能有效:

// 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将结果写入文件。然后触发器设置为更新userLoginEventFirebase 数据库(我从控制台创建)中的位置。接下来,HTTP 请求发出。调用的响应(令牌)是body.app.value('watsonToken','" + body + "');包装令牌的 Angular 值服务。然后fs将所有这些写入我项目中的一个位置。

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

于 2017-10-02T22:50:32.193 回答