【问题标题】:Is Coinmarketcap API blocking requests from Google IP's?Coinmarketcap API 是否会阻止来自 Google IP 的请求?
【发布时间】:2022-01-15 17:46:11
【问题描述】:

我使用了几个月的脚本突然停止工作,因为我在尝试从 Google Apps 脚本访问 Coinmarketcap API 时收到403。我的 API 密钥没有任何问题,因为我在本地机器的 Python 脚本中尝试了相同的请求,并且成功了。

这是我的 Google Apps 脚本:

function coin_price() {
    const myGoogleSheetName =
      SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tokens')
    
    // Call CoinMarketCap and let them know who you are.
    const coinMarketCapAPICall = {
      method: 'GET',
      uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
      qs: {
        start: '1',
        limit: '5000',
        convert: 'USD',
      },
      headers: { 'X-CMC_PRO_API_KEY': '##########' },
      json: true,
      gzip: true,
      muteHttpExceptions: true
    }
  
    // Put the coin symbols that you want to follow here.
    const myCoinSymbols = ['BTC', 'ETH']
    
    // Let's itereate 
    for (let i = 0; i < myCoinSymbols.length; i++) {
      const coinSymbol = myCoinSymbols[i]
      console.log(coinSymbol)
      const coinMarketCapUrl = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${coinSymbol}`
      const result = UrlFetchApp.fetch(coinMarketCapUrl, coinMarketCapAPICall)
      const txt = result.getContentText()
      console.warn(txt)
      const d = JSON.parse(txt)
      const row = i + 2
      // Puts a column of at symbols into the sheet at A2.
      myGoogleSheetName.getRange(row, 1).setValue(coinSymbol)
      // Puts a column of current market price's in dollars into the sheet at B3.
      myGoogleSheetName
        .getRange(row, 2)
        .setValue(d.data[coinSymbol].quote.USD.price)
  
    }
  }

这是我得到的错误:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: ##########
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

Coinmarketcap 是否会屏蔽 Google IP?

【问题讨论】:

  • 很遗憾,在 Google Apps Script 的 UrlFetchApp 中,没有 uriqsjsongzip 的属性。那么这与您当前的问题有关吗? Ref我很担心这个。
  • 这个确切的代码曾经可以工作,但我会尝试不使用这些属性。
  • 脚本再次运行。所以它一定是 Coinmarketcap API 的一个临时块。仍然不确定它为什么会发生,所以我会留下这个问题。

标签: api google-apps-script coinmarketcap


【解决方案1】:

这是 UrlFetchApp 的常见问题,因为所有请求都来自同一个 IP 池。

您有时可以使用exponential backoff 和二项式概率论来解决这个问题(也就是尝试多次,直到它起作用)

我设法让它以这种方式与 Binance API 一起工作(完整的 github repo here),但想法是这样的:

function exponentialBackoff(url, params) {
  for (let count = 0; count < 50; count++) { // choose your amount
    try {
      Utilities.sleep((count * count) + (Math.random() * 1000))
      const response = UrlFetchApp.fetch(`${endpoint}${query}`, params)
    }    
    catch(e) {
      console.info(e)
      continue
    }
    if (response.getResponseCode() === 200) {
      return response
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2022-01-20
    • 2012-05-02
    相关资源
    最近更新 更多