首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不允许使用呋喃HTTP方法

不允许使用呋喃HTTP方法
EN

Stack Overflow用户
提问于 2022-11-22 04:10:03
回答 1查看 65关注 0票数 0

在这里输入图像描述

在这里输入图像描述

以上两个错误显示了细节。

代码语言:javascript
复制
/* create-item.js */

import { useState } from 'react'
import { ethers } from 'ethers'
import { create as ipfsHttpClient } from 'ipfs-http-client'
import { useRouter } from 'next/router'
import Web3Modal from 'web3modal'

const client = ipfsHttpClient('https://ipfs.infura.io:5001/api/v0')

import {
  nftaddress, nftmarketaddress  
} from '../config'

import NFT from '../artifacts/contracts/NFT.sol/NFT.json'
import Market from '../artifacts/contracts/NFTMarket.sol/NFTMarket.json'

export default function CreateItem () {
  const [fileUrl, setFileUrl] = useState(null)
  const [formInput, updateFormInput] = useState({ price: '', name: '', description: '' })
  const router = useRouter()

  async function onChange(e) {
    const file = e.target.files[0]
    try {
      const added = await client.add(
        file,
        {
          progress: (prog) => console.log('received: ${prog}')
        }
      )
      const url = 'https://ipfs.infura.io/ipfs/${added.path}'
      setFileUrl(url)
    } catch (e) {
      console.log(e)  
    }
  }
  
  async function createItem() {
    const { name, description, price } = formInput
    if (!name || !description || !price || !fileUrl) return
    const data = JSON.stringify({
      name, description, image: fileUrl
    })

    try {
      const added = await client.add(date)
      const url = 'https://ipfs.infura.io/ipfs/${added.path}'
      /* */
      createSale(url)
    } catch (error) {
      console.log('Error uploading file: ', error)
    }
  }

  async function createSale() {
    const Web3Modal = new Web3Modal()
    const connection = await web3modal.connect()
    const provider = new ethers.providers.Web3Provider(connection)
    const signer = provider.getSigner()

    let contract = new ethers.Contract(nftaddress, NFT.abi, signer)
    let transaction = await contract.createToken(url)
    let tx = await transaction.wait()

    let event = tx.events[0]
    let value = event.args[2]
    let tokenId = value.toNumber()

    const price = ethers.utils.parseUnits(formInput.price, 'ether')

    contract = new ethers.Contract(nftmarketaddress, Market.abi, signer)
    let listingPrice = await contract.getListingPrice()
    listingPrice = listingPrice.toString()

    transaction = await contract.createMarketItem(
      nftaddress, tokenId, price, { value: listingPrice }
    )
    await transaction.wait()
    router.push('/')
  }

  return (
    <div className="flex justify-center">
      <div className="w-1/2 flex flex-col pb-12">
        <input
          placeholder="Asset Name"
          className="mt-8 border rounded p-4"
          onChange={e => updateFormInput({ ...formInput, name: e.target.value })}
        />
        <textarea
          placeholder="Asset Description"
          className="mt-2 border rounded p-4"
          onChange={e => updateFormInput({ ...formInput, description: e.target.value })}
        />
        <input
          placeholder="Asset Price in Matic"
          className="mt-2 border rounded p-4"
          onChange={e => updateFormInput({ ...formInput, price: e.target.value })}
        />
        <input
          type="file"
          name="Asset"
          className='my-4'
          onChange={onChange}
        />
        {
          fileUrl && (
            <img className="rounded mt-4" width="350" src={fileUrl} />
          )
        }
        <button
          onClick={createItem}
          className="font-bold mt-4 bg-pink-500 text-white rounded p-4 shadow-lg"
        >
          Create Digital Asset
        </button>
      </div>
    </div>
  )

}

我认为我的愤怒是错误的设置。

问题:加载资源失败:服务器响应状态为401 (未经授权)

我试着设置projectID和来自呋喃的秘密密钥,但是它没有起作用。

预期:

  1. 看来他们已经上了v3 -我应该把这个移开
  2. 错误(和文档)表明我正在使用GET - change来发布。

参考资料:https://www.youtube.com/watch?v=GKJBEEXUha0

然而,我失去了设置它的方式。有人能给我提个主意吗?

EN

回答 1

Stack Overflow用户

发布于 2022-11-23 03:16:48

您丢失了infura.io所需的项目id,第一个屏幕截图将返回该id。

按照样本请求提供的infura.io,在发送请求时需要授权头,您可能需要使用以下代码重新实现ipfsHttpClient。

另外,在旧的ipfs中报告了一个错误--http-client,它不能正确地传递授权头,但是已经在v44.0.1中修复了,请确保您使用的也是这个包的最新版本。

对于第二个屏幕截图,/api/v0/add是infura.io定义的POST方法,将URL粘贴到浏览器结果中,以触发GET方法

代码语言:javascript
复制
const client = ipfsHttpClient({
    host: 'https://ipfs.infura.io:5001/api/v0',
    headers: {
                Authorization:
                    'Your infura token here'
            }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74527423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档