使用返回401授权错误的axios编写get,除非在此之前运行curl请求

vof42yt1  于 2023-01-05  发布在  iOS
关注(0)|答案(1)|浏览(145)

我正在开发一个叫做共价API的小型网络应用程序。
具体来说,我使用的是“GET地址令牌余额”端点。
我根据他们的文档编写了请求,如下所示:

const response = await axios.get(
      `https://covalenthq.com/api/v1/${chainId}/address/${address}/balances_v2/`,
      {
        headers: {
            'Authorization': 'Basic ' + btoa(authString),
        },
      }
    );

该请求通常返回401授权错误<?xml version='1.0' encoding='UTF-8'?><Error><Code>AuthenticationRequired</Code><Message>Authentication required.</Message></Error>
然而,当我在终端中通过cURL请求运行完全相同的请求,然后在我的网站上运行该请求时,它工作得很好。我很感激任何关于为什么会发生这种情况的指导,因为我对Web开发很陌生。谢谢!

0qx6xfy6

0qx6xfy61#

此代码有效

const axios = require('axios')
const base64 = require('base-64');

const API_KEY = '************ your API Key ***********'
const getData = async (chainId, address) => {
    try {
        const url = `https://api.covalenthq.com/v1/${chainId}/address/${address}/balances_v2/`
        const resp = await axios.get(
            url,
            {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Basic ' + base64.encode(`${API_KEY}:`)
                }
            }
        );
        return Promise.resolve(resp.data);
    } catch (err) {
        // Handle Error Here
        console.error(err);
    }
};
getData(1, 'demo.eth')
    .then(result => {
        console.log(JSON.stringify(result, null, 4));
    })
    .catch(error => {
        console.error(error);
    });

结果

{
   "data" : {
      "address" : "0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de",
      "chain_id" : 1,
      "items" : [
         {
            "balance" : "4843997835959514312853",
            "balance_24h" : "4843997835959514312853",
            "contract_address" : "0xf8c3527cc04340b208c854e985240c02f7b7793f",
            "contract_decimals" : 18,
            "contract_name" : "Frontier Token",
            "contract_ticker_symbol" : "FRONT",
            "last_transferred_at" : "2022-09-27T15:29:11Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xf8c3527cc04340b208c854e985240c02f7b7793f.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 868.6257,
            "quote_24h" : null,
            "quote_rate" : 0.17932,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "566909001132176043",
            "balance_24h" : "566909001132176043",
            "contract_address" : "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
            "contract_decimals" : 18,
            "contract_name" : "Ether",
            "contract_ticker_symbol" : "ETH",
            "last_transferred_at" : "2022-12-21T06:52:47Z",
            "logo_url" : "https://www.covalenthq.com/static/images/icons/display-icons/ethereum-eth-logo.png",
            "native_token" : true,
            "nft_data" : null,
            "quote" : 678.24426,
            "quote_24h" : 676.59656,
            "quote_rate" : 1196.39,
            "quote_rate_24h" : 1193.4835,
            "supports_erc" : null,
            "type" : "cryptocurrency"
         },
         {
            "balance" : "1496578332324",
            "balance_24h" : "1496578332324",
            "contract_address" : "0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9",
            "contract_decimals" : 8,
            "contract_name" : "Compound USDT",
            "contract_ticker_symbol" : "cUSDT",
            "last_transferred_at" : "2020-06-16T14:50:10Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 224.65796,
            "quote_24h" : null,
            "quote_rate" : 0.01501144,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "180061509259325208243",
            "balance_24h" : "180061509259325208243",
            "contract_address" : "0xfc1e690f61efd961294b3e1ce3313fbd8aa4f85d",
            "contract_decimals" : 18,
            "contract_name" : "Aave Interest bearing DAI",
            "contract_ticker_symbol" : "aDAI",
            "last_transferred_at" : "2020-07-07T04:57:49Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xfc1e690f61efd961294b3e1ce3313fbd8aa4f85d.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 180.6017,
            "quote_24h" : null,
            "quote_rate" : 1.003,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "149718525513119178237",
            "balance_24h" : "149718525513119178237",
            "contract_address" : "0xc25a3a3b969415c80451098fa907ec722572917f",
            "contract_decimals" : 18,
            "contract_name" : "Curve.fi DAI/USDC/USDT/sUSD",
            "contract_ticker_symbol" : "crvPlain3andSUSD",
            "last_transferred_at" : "2020-05-09T16:04:17Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xc25a3a3b969415c80451098fa907ec722572917f.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 158.70163,
            "quote_24h" : null,
            "quote_rate" : 1.06,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "634985147680",
            "balance_24h" : "634985147680",
            "contract_address" : "0x5d3a536e4d6dbd6114cc1ead35777bab948e3643",
            "contract_decimals" : 8,
            "contract_name" : "Compound Dai",
            "contract_ticker_symbol" : "cDAI",
            "last_transferred_at" : "2020-06-16T14:41:37Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x5d3a536e4d6dbd6114cc1ead35777bab948e3643.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 140.4208,
            "quote_24h" : null,
            "quote_rate" : 0.02211403,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "498617081",
            "balance_24h" : "498617081",
            "contract_address" : "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5",
            "contract_decimals" : 8,
            "contract_name" : "Compound Ether",
            "contract_ticker_symbol" : "cETH",
            "last_transferred_at" : "2021-10-30T13:59:21Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 119.96727,
            "quote_24h" : null,
            "quote_rate" : 24.06,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "106915682698270160455",
            "balance_24h" : "106915682698270160455",
            "contract_address" : "0x5bc25f649fc4e26069ddf4cf4010f9f706c23831",
            "contract_decimals" : 18,
            "contract_name" : "DefiDollar",
            "contract_ticker_symbol" : "DUSD",
            "last_transferred_at" : "2020-10-12T08:53:11Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x5bc25f649fc4e26069ddf4cf4010f9f706c23831.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 101.23974,
            "quote_24h" : null,
            "quote_rate" : 0.946912,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "15620651498422024764",
            "balance_24h" : "15620651498422024764",
            "contract_address" : "0xba100000625a3754423978a60c9317c58a424e3d",
            "contract_decimals" : 18,
            "contract_name" : "Balancer",
            "contract_ticker_symbol" : "BAL",
            "last_transferred_at" : "2020-10-20T22:59:53Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xba100000625a3754423978a60c9317c58a424e3d.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 80.91497,
            "quote_24h" : null,
            "quote_rate" : 5.18,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "8697000000000000000000",
            "balance_24h" : "8697000000000000000000",
            "contract_address" : "0x44709a920fccf795fbc57baa433cc3dd53c44dbe",
            "contract_decimals" : 18,
            "contract_name" : "DappRadar",
            "contract_ticker_symbol" : "RADAR",
            "last_transferred_at" : "2021-12-15T11:50:40Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x44709a920fccf795fbc57baa433cc3dd53c44dbe.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 77.16787,
            "quote_24h" : null,
            "quote_rate" : 0.00887293,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "732502744040979731",
            "balance_24h" : "732502744040979731",
            "contract_address" : "0x3adb04e127b9c0a5d36094125669d4603ac52a0c",
            "contract_decimals" : 18,
            "contract_name" : "Gro Vault Token",
            "contract_ticker_symbol" : "GVT",
            "last_transferred_at" : "2022-07-02T22:45:15Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x3adb04e127b9c0a5d36094125669d4603ac52a0c.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 69.85146,
            "quote_24h" : null,
            "quote_rate" : 95.36,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "54742710353338684662",
            "balance_24h" : "54742710353338684662",
            "contract_address" : "0x4fabb145d64652a948d72533023f6e7a623c7c53",
            "contract_decimals" : 18,
            "contract_name" : "Binance USD",
            "contract_ticker_symbol" : "BUSD",
            "last_transferred_at" : "2020-09-20T04:04:18Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x4fabb145d64652a948d72533023f6e7a623c7c53.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 54.797455,
            "quote_24h" : null,
            "quote_rate" : 1.001,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "51463363958932891486",
            "balance_24h" : "51463363958932891486",
            "contract_address" : "0x6b175474e89094c44da98b954eedeac495271d0f",
            "contract_decimals" : 18,
            "contract_name" : "Dai Stablecoin",
            "contract_ticker_symbol" : "DAI",
            "last_transferred_at" : "2021-10-30T12:05:47Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x6b175474e89094c44da98b954eedeac495271d0f.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 51.463364,
            "quote_24h" : null,
            "quote_rate" : 1,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "stablecoin"
         },
         {
            "balance" : "891111572302786779390",
            "balance_24h" : "891111572302786779390",
            "contract_address" : "0x00a8b738e453ffd858a7edf03bccfe20412f0eb0",
            "contract_decimals" : 18,
            "contract_name" : "AllianceBlock Token",
            "contract_ticker_symbol" : "ALBT",
            "last_transferred_at" : "2021-06-03T07:27:42Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0x00a8b738e453ffd858a7edf03bccfe20412f0eb0.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 37.78569,
            "quote_24h" : null,
            "quote_rate" : 0.04240287,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         },
         {
            "balance" : "20097409502379000020",
            "balance_24h" : "20097409502379000020",
            "contract_address" : "0xba11d00c5f74255f56a5e366f4f77f5a186d7f55",
            "contract_decimals" : 18,
            "contract_name" : "BandToken",
            "contract_ticker_symbol" : "BAND",
            "last_transferred_at" : "2020-05-09T16:15:54Z",
            "logo_url" : "https://logos.covalenthq.com/tokens/1/0xba11d00c5f74255f56a5e366f4f77f5a186d7f55.png",
            "native_token" : false,
            "nft_data" : null,
            "quote" : 27.734425,
            "quote_24h" : null,
            "quote_rate" : 1.38,
            "quote_rate_24h" : null,
            "supports_erc" : [
               "erc20"
            ],
            "type" : "cryptocurrency"
         }
....

相关问题