0

I am trying to get data from a udemy API. I can get it with cURL in the console but not with fetch. Please can anyone look at my code and let me know what I am doing wrong?

const fetch = require("node-fetch");
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;

const path = require("path");

app.use(express.static("public"));
app.use(express.static(path.resolve(__dirname, "public")));
app.get("/", (req, res) => {
  res.sendFile("index.html", { root: __dirname });
});

app.get("/contact", (req, res) => {
  res.sendFile("contact.html", { root: __dirname });
});
const client_id = "client_id";
const client_secret = "client_secret";
const client = Buffer.from(`${client_id}:${client_secret}`).toString("base64");
fetch("https://www.udemy.com/api-2.0/courses/", {
  headers: {
    Authorization: `Basic ${client}`,
  },
})
  .then((res) => res.json())
  .then((json) => console.log(json))
  .catch((err) => {
    console.log(err);
  });
Phil
  • 157,677
  • 23
  • 242
  • 245
Develop96
  • 1
  • 3
  • 1
    If doesn't look like you're passing the `client_id`/`client_secret` to the `fetch` request. I assume you're missing an auth header? – fubar Jul 01 '22 at 01:59
  • Can you provide the equivalent curl request? it looks like you should at least be getting an error or response when you run this code. – Matt Jul 01 '22 at 02:22
  • Yes because I was playing with it a lot. but I had the header and it still gives me: you do not have the permission to perform this action. I am trying to update the post. – Develop96 Jul 01 '22 at 02:24
  • 1
    curl --user client_id:client_secret https://www.udemy.com/api-2.0/courses/ here is the equivalent curl that is working just fine in the console and gives me the data. – Develop96 Jul 01 '22 at 02:25
  • Run `curl` with the verbose flag and check the value of the `Authorization` request header, eg `curl -v -u "client_id:client_secret" "https://www.udemy.com/api-2.0/courses/" -o /dev/null`. Then compare it to the base64 value you've calculated for `client` in your code, eg `console.log("client", client)`. If they're different, use a base64 decoder to compare – Phil Jul 01 '22 at 02:40
  • I compared my conversion of client to the header they had on the API's website and it's exactly the same. That's why I am so confused. Is there a way I can change this code using formData? – Develop96 Jul 01 '22 at 02:49
  • I didn't ask anything about the API website, I asked you to compare your curl and fetch header values. Are they **exactly** the same? Also, no, FormData is for request payloads but yours is a GET request – Phil Jul 01 '22 at 02:54
  • okay, i am going to do that. – Develop96 Jul 01 '22 at 02:57
  • I just checked, and they are not. – Develop96 Jul 01 '22 at 03:02
  • I meant they are not the same. The header from curl is different. – Develop96 Jul 01 '22 at 03:03
  • That means whatever values you actually have for `client_id` and / or `client_secret` are not the same ones you're using in `curl`. Use a [Base64 Decoder](https://www.base64decode.org/) to compare – Phil Jul 01 '22 at 03:05
  • I just copied and pasted the authorization in my Phil and it's working. Thank you very much – Develop96 Jul 01 '22 at 03:11

0 Answers0