Already add project id and secret key, but still get 401 (Unauthorized)

I added project id and secret key, but still getting 401.

Screenshot 2022-12-07 150626

Here my ipfs.js

const { create } = require('ipfs-http-client');

const projectId = '2c011...'

const projectSecret = '7c6...'

const infura = {

  host: 'ipfs.infura.io',

  port: 5001,

  protocol: 'https',

  timeout: 15000,

  method:'POST',

  auth : projectId +':' +projectSecret,

};

const ipfs = create(infura);

async function uploadToIPFS(object) {

  console.log('🛰  Sending to IPFS...');

  try {

    const ipfsResult = await ipfs.add(object);

    const uploadPath = ipfsResult.path;

    return uploadPath;

  } catch (err) {

    console.log("efefeffef");

    console.log(err.message);

    return '';

  }

}

module.exports = {

uploadToIPFS,

};

Hey @Tony_Seng welcome to the community.
The auth header needs to be passed as a base64 string. You can use something similar to the below:


const auth =

'Basic ' + Buffer.from(projectId + ':' + projectSecret).toString('base64')

const client =  create({

    host: 'ipfs.infura.io',

    port: 5001,

    protocol: 'https',

    headers: {

      authorization: auth

    }

  })

thanks, it works like charm :slight_smile: