【问题标题】:Can't fetch data from Marvel API status of 401无法从 Marvel API 状态 401 获取数据
【发布时间】:2018-11-03 23:14:31
【问题描述】:

这是我第一次使用 Marvel API,但由于某种原因,它只是在控制台中向我发送了 401(未授权)的错误状态。我已经包含了 API 密钥。但仍然得到错误

import React,{Component} from 'react'
 import axios from 'axios';

 class Home extends Component {
    componentDidMount(){
        let api = 'APIHERE'

        axios.get(`https://gateway.marvel.com/v1/public/comics?apikey=${api}`)
        .then(res=>{
            console.log(res)
        }).catch(error =>{
            console.log(error)
        })
    }
    render(){    
         return(
             <div className = "container">
                <h4>Characters</h4> 
            </div>
         )
     }
 }

 export default Home;

【问题讨论】:

  • 如果您收到 401,您使用了错误的 api 密钥,请使用该端点的公钥

标签: javascript reactjs api axios


【解决方案1】:

您需要添加时间戳和哈希键以及 apiKey 才能使 API 调用成功。

【讨论】:

    【解决方案2】:

    首先,您必须按照 api 文档中的说明安装 md5 。其次,您必须发送 3 个参数才能获得身份验证。

    ts : 时间戳

    apikey:公钥

    哈希:md5(ts+publickey+privatekey)

    如果您使用 react-native :尝试使用 hex_md5==>https://www.npmjs.com/package/react-native-md5

    或者尝试安装 md5 库。

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多