Go语言获取需要权限认证的API数据

公开API

首先看一个请求json数据的例子,一个公开的API,谁都可以访问,无需权限认证,
数据来源:https://api.coinmarketcap.com/v2/ticker/


接下来看如何请求需要权限认证的API数据,数据来源为以下网址,
https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest
https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
这两个API是需要权限认证的,也就是需要APIkey,没有权限的时候,你直接点击这两个网址,返回的数据是这样的,

{
    "status": {
        "timestamp": "2018-12-11T00:53:33.535Z",
        "error_code": 401,
        "error_message": "API key missing.",
        "elapsed": 0,
        "credit_count": 0
    }
}

也就是返回了错误,错误信息为"API key missing."API秘钥缺失。
到这个网站https://coinmarketcap.com/注册一个账号,就可以免费申请一个APIkey,接下来就可以用申请到的APIkey去正常获取上面两个网址的数据,通过权限认证以后获取到的数据是这样的,

{
    "status": {
        "timestamp": "2018-12-11T00:42:59.418Z",
        "error_code": 0,
        "error_message": null,
        "elapsed": 9,
        "credit_count": 1
    },
    "data": [
        {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            "slug": "bitcoin",
            "circulating_supply": 17416675,
            "total_supply": 17416675,
            "max_supply": 21000000,
            "date_added": "2013-04-28T00:00:00.000Z",
            "num_market_pairs": 6684,
            "tags": [
                "mineable"
            ],
            "platform": null,
            "cmc_rank": 1,
            "last_updated": "2018-12-11T00:41:22.000Z",
            "quote": {
                "USD": {
                    "price": 3490.26054654,
                    "volume_24h": 4958844740.50419,
                    "percent_change_1h": -0.352116,
                    "percent_change_24h": -3.79482,
                    "percent_change_7d": -10.3529,
                    "market_cap": 60788733604.40956,
                    "last_updated": "2018-12-11T00:41:22.000Z"
                }
            }
        },
        {
            "id": 52,
            "name": "XRP",
            "symbol": "XRP",
            "slug": "ripple",
            ………………………………………底下数据省略…………………………………………

接下来就来看一下如何获取这种需要权限认证的API数据,下面以Go语言举栗子,首先到官网查看API文档,https://coinmarketcap.com/api/documentation/v1/#,如何请求API数据已经写得很清楚,不过具体的每种语言的操作步骤不太一样
请求需要认证的API数据
看看Go语言要怎么做,其实就是当你发起一个数据请求的时候要把你的APIkey添加到请求头中,然后这个网站就知道你是有权限的,就会给你正确的数据。

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

var (
	url   = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
	url1  = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/info/"
	url2  = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest"
	mykey = "860da129-69ed-189a-9379-2d48a4c00bd6"
)

func main() {
	//首先创建一个客户端client,
	client := &http.Client{}
	//创建一个请求request,
	reqest, err := http.NewRequest("GET", url, nil)
	//为请求头添加秘钥认证,
	reqest.Header.Set("X-CMC_PRO_API_KEY", mykey)
	//reqest.Header.Add("Accept", "application/json")
	//reqest.Header.Add("Accept-Encoding", "deflate")
	if err != nil {
		fmt.Println(err)
	}
	//执行请求,得到响应数据response,
	resp, err := client.Do(reqest)
	defer resp.Body.Close()
	//读取响应体response的body,用`ioutil.ReadAll`读取,该函数返回结果为byte,
	body, err := ioutil.ReadAll(resp.Body)
	//将byte转为可读取的string类型,打印一下,就会看到得到了正确的响应,
	fmt.Println(string(body))
}

运行结果

{
    "status": {
        "timestamp": "2018-12-11T01:13:02.768Z",
        "error_code": 0,
        "error_message": null,
        "elapsed": 10,
        "credit_count": 1
    },
    "data": [
        {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            "slug": "bitcoin",
            "circulating_supply": 17416725,
            "total_supply": 17416725,
            "max_supply": 21000000,
            "date_added": "2013-04-28T00:00:00.000Z",
            "num_market_pairs": 6684,
            "tags": [
                "mineable"
            ],
            "platform": null,
            "cmc_rank": 1,
            "last_updated": "2018-12-11T01:11:20.000Z",
            "quote": {
                "USD": {
                    "price": 3477.56550428,
                    "volume_24h": 4902670930.10955,
                    "percent_change_1h": -0.418618,
                    "percent_change_24h": -4.16081,
                    "percent_change_7d": -10.7638,
                    "market_cap": 60567802057.53108,
                    "last_updated": "2018-12-11T01:11:20.000Z"
                }
            }
        },
        {
            "id": 52,
            "name": "XRP",
            "symbol": "XRP",
            …………………略………………………………
            …………………略………………………………
            …………………略………………………………

这里最后的得到的最终数据是body,类型为byte,实际开发中,还需要将byte转为结构体,才可以应用,至于如何将byte数据转为结构体,可参考这篇文章Go语言如何解析复杂json数据

相关文章: