strapi-sdk-javascript icon indicating copy to clipboard operation
strapi-sdk-javascript copied to clipboard

Node.js upload issue with downloaded file from facebook oauth

Open kevinvugts opened this issue 3 years ago • 3 comments

https://github.com/strapi/strapi-sdk-javascript/blob/bd442dad2df8c8442f544e079c96bccb9059307b/src/lib/sdk.ts#L336-L369

I have read this in another issue and tried the suggested in the above link / gist. However, how would we do this if we don't have a local file but a downloaded file instead? In my case I want a facebook user picture to be uploaded to strapi. In all cases it doesn't work. Tried to send it as a buffer as suggested in the docs.

Looking forward for some help.

Code:

        // PARSE FACEBOOK PROVILE PICTURE
        const files = await fetch(`${profile.avatar}`).then((res) =>
          res.buffer()
        );

        console.log("FILES", files);

        // CREATE FORM DATA
        form.append("files", buffer, "test.jpg");
        form.append("ref", "user");
        form.append("refId", createdUser.id);
        form.append("field", "avatar");
        form.append("source", "users-permissions");

        // GET USER JWT TO AUTHORIZE UPLOAD REQUEST
        const userJwt = await axios
          .get(
            `${process.env.SERVER_URL}auth/${provider}/callback?access_token=${access_token}`
          )
          .then((res) => res.data);

        // UPLOAD AVATAR FROM FACEBOOK
        await axios
          .post(`${process.env.SERVER_URL}upload`, form, {
            headers: {
              Authorization: `Bearer ${userJwt.jwt}`,
              ...form.getHeaders(),
            },
          })
          .then((res) => console.log("NEW PROFILE PICTURE UPLOAD"))
          .catch((error) => {
            if (error.response) {
              console.log("err", JSON.stringify(error.response.data));
            }

            console.log("ERROR", JSON.stringify(error));
          });

Environement:

Running on the latest version of strapi.

kevinvugts avatar Oct 21 '20 13:10 kevinvugts