简体   繁体   中英

IBM Cloud Functions Calling API with authentication / node.js

For most of you reading this, it is portably the most basic question and done within 2 minutes.. Maybe someone got the time to provide the code for me or can recommend a resource where this is being explained for an absolute beginner.

I want to call an API from IBM cloud functions that requires authentication.

I got this code from an IBM video tutorial with that I can call any open API:

let rp = require('request-promise')
  
function main(params) {
    if (params.actionA == 'joke') {
         const options = {
       uri: "http://api.icndb.com/jokes/random",
       json: true
       }
    return rp(options)
    .then(res => {
       return { response: res }
     })
} else if (params.actionB == 'fact') {
        const options = {
    uri: "https://catfact.ninja/fact",
    json: true
    }
return rp(options)
.then(res => {
        return { response: res }
})
}
} 

I want to keep the joke API but want to exchange the Cat fact API with this inspirational quote API (which needs authenticaion): https://english.api.rakuten.net/HealThruWords/api/universal-inspirational-quotes/details

I can get this node.js code from rakuten to call the quote api.

var http = require("https");

var options = {
    "method": "GET",
    "hostname": "healthruwords.p.rapidapi.com",
    "port": null,
    "path": "/v1/quotes/?id=731&t=Wisdom&maxR=1&size=medium",
    "headers": {
        "x-rapidapi-host": "healthruwords.p.rapidapi.com",
        "x-rapidapi-key": "api key here",
        "useQueryString": true
    }
};

var req = http.request(options, function (res) {
    var chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function () {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
    });
});

req.end();

How can I incorporate it into the if function? I want to use that function with watson assistant, which works well with the current code. I just need the catfact api exchanged by the quote api.

The two code snippets that you have provided use different modules to do the http request. That is probably why it looks a bit complicated.

The first step to change the behaviour in a way that you have described, is to replace the complete options in the else branch with the options from you second code snippet.

The second step is to provide the necessary api key for the quote URL as a parameter to the function.

Can you give this code snippet below a try, after adding an additional parameter apiKey to the function. I have not tested the code below, but that's how I would do it. Please let me know if it worked for you and I can improve the answer on your feedback.

let rp = require('request-promise')

function main(params) {
    if (params.actionA == 'joke') {
        const options = {
            uri: "http://api.icndb.com/jokes/random",
            json: true
        }
        return rp(options)
            .then(res => {
                return { response: res }
            })
    } else if (params.actionB == 'fact') {
        const options = {
            "method": "GET",
            "hostname": "healthruwords.p.rapidapi.com",
            "port": null,
            "uri": "/v1/quotes/?id=731&t=Wisdom&maxR=1&size=medium",
            "headers": {
                "x-rapidapi-host": "healthruwords.p.rapidapi.com",
                "x-rapidapi-key": params.apiKey,
                "useQueryString": true
            }
        }
        return rp(options)
            .then(res => {
                return { response: res }
            })
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM