首页 > 解决方案 > 在 android 中使用 youtube data api v3 进行 youtube 搜索

问题描述

我正在开发具有此功能的新应用程序:
-用户可以从应用程序中搜索 youtube 视频 ..
我尝试使用 youtube api ..我在 google 控制台中创建了项目,生成了带有 android 应用程序限制的 api_key(编写了应用程序包和 SHA-1证书指纹)。
我使用了这个链接: https://www.googleapis.com/youtube/v3/search?part=snippet&q=blabla&type=video&key=[api_key]
我来自:https://developers.google.com/youtube/v3 /docs/search/list
但不幸的是我得到了这个回复,我知道为什么

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "ipRefererBlocked",
    "message": "The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions.",
    "extendedHelp": "https://console.developers.google.com/apis/credentials?project=953519755004"
   }
  ],
  "code": 403,
  "message": "The request did not specify any Android package name or signing-certificate fingerprint. Please ensure that the client is sending them or use the API Console to update your key restrictions."
 }
}

我希望任何人都可以拯救我的一天

标签: androidyoutube-data-apiandroid-youtube-apiapi-keygoogle-console-developer

解决方案


您可能需要设置两个请求标头:

Youtube API 密钥

youTube = new YouTube.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) throws IOException { String packageName = context.getPackageName(); String SHA1 = getSHA1(packageName );

    request.getHeaders().set("X-Android-Package", packageName);
    request.getHeaders().set("X-Android-Cert",SHA1);
}

}).setApplicationName(appName).build();


推荐阅读