Advertisement
LEO0506

API authentication

Jan 26th, 2024 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import express from "express";
  2. import axios from "axios";
  3.  
  4. const app = express();
  5. const port = 3001;
  6. const API_URL = "https://secrets-api.appbrewery.com";
  7.  
  8. // data to api
  9. const yourUsername = "LeonardoTMV";
  10. const yourPassword = "6221Bh[]";
  11. const yourAPIKey = "7dbef3e7-50a6-44ed-b483-520453b0f888";
  12. const yourBearerToken = "e3f37d3f-d340-4e10-82dc-eab766bc7a05";
  13.  
  14. app.use(express.urlencoded({extended:true}));
  15.  
  16. app.get("/", (req, res) => {
  17.   res.render("index.ejs", {content: "API Response." });
  18. });
  19.  
  20. app.get("/noAuth", async (req, res) => {
  21.   try{
  22.     const resultGot = await axios.get(API_URL+"/random");
  23.     const resultToShow = resultGot.data;
  24.     console.log(req.body);
  25.     console.log(resultToShow);
  26.     res.render("index.ejs", {
  27.       content: JSON.stringify(resultToShow)
  28.     });
  29.   }catch(error){
  30.     console.log(API_URL + "/random")
  31.     res.status(404).send(error.message);
  32.     console.log(`error: ${error.message}`);
  33.   }
  34. });
  35.  
  36. app.get("/basicAuth", async (req, res) => {
  37.   try{
  38.     const resultGot = await axios.post(API_URL+"/all?page=2",
  39.     {
  40.       auth: {
  41.           username: yourUsername,
  42.           password: yourPassword,
  43.         },
  44.     });
  45.     const resultToShow = resultGot.data;
  46.     console.log(req.body);
  47.     console.log(resultToShow);
  48.     res.render("index.ejs", {
  49.       content: JSON.stringify(resultToShow)
  50.     });
  51.   }catch(error){
  52.     res.status(404).send(error.message);
  53.     console.log(`error: ${error.message}`);
  54.   }
  55. });
  56. app.get("/apiKey", async (req, res) => {
  57.   try{
  58.     const resultGot = await axios.get(API_URL+"/filter",
  59.     {
  60.       params: {
  61.         score: 5,
  62.         apiKey: yourAPIKey
  63.       }
  64.     });
  65.     const resultToShow = resultGot.data;
  66.     res.render("index.ejs", {
  67.       content: JSON.stringify(resultToShow)
  68.     });
  69.   }catch(error){
  70.     res.status(404).send(error.message);
  71.     console.log(`error: ${error.message}`);
  72.   }
  73. });
  74.  
  75. app.get("/bearerToken", async (req, res) => {
  76.   try{
  77.     const resultGot = await axios.get(API_URL+"/secrets/2",
  78.     {
  79.       headers: {
  80.         Authorization: yourBearerToken
  81.       }
  82.     });
  83.     const resultToShow = resultGot.data;
  84.     res.render("index.ejs", {
  85.       content: JSON.string(resultToShow)
  86.     });
  87.   }catch(error){
  88.     res.status(404).send(error.message);
  89.     console.log(`error: ${error.message}`);
  90.   }
  91. });
  92.  
  93. app.listen(port, () => {
  94.   console.log(`Server is running on port ${port}`);
  95. });
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement