首页 > 解决方案 > 使用 urllib 和 json 读取 Python google api

问题描述

我的代码很短,但它不工作,但在 youtube 视频中它工作

import urllib.request
import json

key = "AIzaSyCyTALBjNAEp-Vr5GkilEnLzH0y1Tw6SC4"
data = urllib.request.urlopen("www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=pewdiepie&key="+key).read()
subs = json.loads(data)['items'][0]["statistics"]['subscriberscount']
print(subs)

标签: pythonjsongoogle-api

解决方案


您需要创建自己的 API 密钥

https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=pewdiepie&key=AIzaSyCyTALBjNAEp-Vr5GkilEnLzH0y1Tw6SC4

节目

  "error": {
    "code": 403,
    "message": "YouTube Data API v3 has not been used in project 195563281908 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=195563281908 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "errors": [
      {
        "message": "YouTube Data API v3 has not been used in project 195563281908 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=195563281908 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
        "domain": "usageLimits",
        "reason": "accessNotConfigured",
        "extendedHelp": "https://console.developers.google.com"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

在这里您可以找到如何创建自己的 API 密钥: https ://developers.google.com/youtube/v3/getting-started


推荐阅读