# Solid-client-authn-node / solid-client authorized requests

**URL:** <https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684>\
**Category:** Build a Solid App\
**Created:** [September 22, 2021, 3:44pm UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684 "2021-09-22T15:44:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 22, 2021, 3:44pm UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/1 "2021-09-22T15:44:29Z")

</div>

Hi, I’m trying out the solid-client-authn-node application for a Node.js backend service.

I’ve followed all the Inrupt tutorials but I got stuck with the authorized requests.

[https://docs.inrupt.com/developer-tools/javascript/client-libraries/using-libraries/](https://docs.inrupt.com/developer-tools/javascript/client-libraries/using-libraries/)

As mentioned in the tutorials, while performing authorized requests we should always pass the fetch function from the authorized Session to the solid-client methods.

So in my codebase I wanted to do that, but I keep getting errors while passing the fetch function.

```auto
const express = require("express");
const cookieSession = require("cookie-session");

const {
  getSessionFromStorage,
  getSessionIdFromStorageAll,
  Session
} = require("@inrupt/solid-client-authn-node");

const {
  access,
  getSolidDataset
} = require("@inrupt/solid-client")

const app = express();
const port = 3000;

app.use(express.json());
app.use(express.urlencoded());

// The following snippet ensures that the server identifies each user's session
// with a cookie using an express-specific mechanism
app.use(
  cookieSession({
    name: "session",
    // These keys are required by cookie-session to sign the cookies.
    keys: [
      "Required, but value not relevant for this demo - key1",
      "Required, but value not relevant for this demo - key2",
    ],
    maxAge: 24 * 60 * 60 * 1000, // 24 hours
  })
);

// Login endpoint: returns the link to the OIDC provider
app.get('/login', async (req, res) => {
  const session = new Session();
  req.session.sessionId = session.info.sessionId;

  //redirect url and oidc issuer fetched from the request body
  const redirectUrl = "http://localhost:3000/handleRedirect"
  const oidcIssuer = "https://solidcommunity.net"

  //return the link to the OIDC provider
  const returnSolidIdentityProviderLoginUrl = (url) => {
    res.send(url)
  }

  await session.login({
    redirectUrl: redirectUrl,
    oidcIssuer: oidcIssuer,
    clientName: "Covid Safe App Cronos",
    handleRedirect: returnSolidIdentityProviderLoginUrl
  })
});

// OIDC provider redirects back to this endpoint
app.get("/handleRedirect", async (req, res) => {
  let code = req.url.split('?')[1]

  const session = await getSessionFromStorage(req.session.sessionId);
  await session.handleIncomingRedirect(`http://localhost:${port}/handleRedirect?${code}`);

  if (session.info.isLoggedIn) {
    return res.send(`<p>Logged in with the WebID ${session.info.webId}.</p>`)
  }
})

app.get("/fetch", async (req, res) => {
  const session = await getSessionFromStorage(req.session.sessionId);
  const resource = encodeURI("https://warddriesen.solidcommunity.net/private/private_resource")

  if (!session || !session.info.isLoggedIn) {
    return res.status(401)
  }

  if (!resource) {
    return res.status(404).json({ error: "Please pass the (encoded) URL of the Resource you want to fetch!" })
  }

  const data = await getSolidDataset(resource, { fetch: session.fetch })
  res.send(data)
})

```

 ![error](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/2/24a97dae47d039cb84e04ab3b318beeb79d24afc.png)

When I just execute the session fetch function I get the desired result (as shown below). But all the other solid-client functions require a fetch function to be passed in the options object.

```auto
app.get("/fetch", async (req, res) => {
  const session = await getSessionFromStorage(req.session.sessionId);
  const resource = encodeURI("https://warddriesen.solidcommunity.net/private/private_resource")

  if (!session || !session.info.isLoggedIn) {
    return res.status(401)
  }

  if (!resource) {
    return res.status(404).json({ error: "Please pass the (encoded) URL of the Resource you want to fetch!" })
  }

  const result = await (await session.fetch(resource)).text()
  res.send(result)
})

```

Hopefully someone can help me on this one 🙂 !

---

<div class="post-metadata">

**Author:** ![Vincent](https://avatars.discourse-cdn.com/v4/letter/v/e9bcb4/32.png) [@Vincent](https://forum.solidproject.org/u/Vincent)\
**Post date:** [September 22, 2021, 4:52pm UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/2 "2021-09-22T16:52:26Z")

</div>

What is the value of `result` in that last code snippet? And what do you get if you inspect `(await session.fetch(resource)).headers.get("Content-Type")`? Possibly it’s not actually a SolidDataset? (If it is, the Content-Type should be something like `text/turtle`.)

Can you view the resource using [Penny](https://penny.vincenttunru.com/)? (It uses solid-client behind the scenes.)

---

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 23, 2021, 7:27am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/3 "2021-09-23T07:27:47Z")

</div>

Hi @Vincent, thanks for the answer.

So this is the source file that I uploaded on my pod:

 ![source](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/a/adae2d4703613d47d964bafb9452bbd317c75270.png)

This is the result of the last code snippet:  
–SEE BELOW–

While using Penny I’m not able to read the file:  
–SEE BELOW–

Which is kind of weird because I uploaded the same file to my pod hosted on [https://warddriesen.inrupt.net](https://warddriesen.inrupt.net), and now with Penny I can read that file (but still not on my pod hosted on [https://warddriesen.solidcommunity.net](https://warddriesen.solidcommunity.net)):  
–SEE BELOW–

I also tried to fetch the resource from my Inrupt pod with  
`const data = await getSolidDataset(resource, { fetch: session.fetch }) res.send(data)`  
But this code snippet also throws the same exception as displayed above.

The content-type of `(await session.fetch(resource)).headers.get("Content-Type")` is : `application/octet-stream`

EDIT: this forum won’t let me post more than 1 embedded picture in a comment because I’m a new user, so see the comments below 🙂

---

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 23, 2021, 7:30am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/4 "2021-09-23T07:30:21Z")

</div>

This is the result of the last code snippet:

 ![result](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/8/881e7d1bdb73099c4ea3cc081f78157e4b70e270.jpeg)

---

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 23, 2021, 7:31am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/5 "2021-09-23T07:31:04Z")

</div>

While using Penny I’m not able to read the file:

 ![penny](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/c/c239a784cc4e3127fde9452021b724c42a3601d9.jpeg)

---

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 23, 2021, 7:32am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/6 "2021-09-23T07:32:02Z")

</div>

Which is kind of weird because I also uploaded the same file to my pod hosted on [https://warddriesen.inrupt.net](https://warddriesen.inrupt.net), and now with Penny I can read the file (but still not on my pod hosted on [https://warddriesen.solidcommunity.net](https://warddriesen.solidcommunity.net)):

 ![penny_inrupt](https://europe1.discourse-cdn.com/flex017/uploads/solidproject/original/2X/1/11fe5fbd7de6a8d463eb96aa9e55efb939d56a06.jpeg)

---

<div class="post-metadata">

**Author:** ![bourgeoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/bourgeoa/32/291_2.png) [@bourgeoa](https://forum.solidproject.org/u/bourgeoa)\
**Post date:** [September 23, 2021, 10:38am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/7 "2021-09-23T10:38:29Z")

</div>

I checked on the server your file on [https://warddriesen.solidcommunity.net](https://warddriesen.solidcommunity.net) and it is of content-type octet-stream and not text/turtle.  
That means it cannot be parsed as text/turtle.

I suppose your first PUT did miss an explicit text/turtle content-type and your function default to octet-stream.  
You should remove the resource and reload it using exactly the same function on both servers.  
They both use exactly the same NSS v5.6.8.

---

<div class="post-metadata">

**Author:** ![warddriesen](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.solidproject.org/warddriesen/32/2947_2.png) [@warddriesen](https://forum.solidproject.org/u/warddriesen)\
**Post date:** [September 23, 2021, 11:50am UTC](https://forum.solidproject.org/t/solid-client-authn-node-solid-client-authorized-requests/4684/8 "2021-09-23T11:50:07Z")

</div>

Thanks a lot @bourgeoa! That solved the issue 🙂
