我正在开发 Java 代码,以使用服务帐户身份验证检索相应资产 ID 的视频 ID。我遵循了下面提到的步骤。第 1 步:编写一个 Java 程序,以使用服务帐户从 Google 身份验证中检索访问令牌。该代码用于获取access_token:
String EmailId = "XXXXXXXXXXX@developer.gserviceaccount.com";
//passing Scope
@SuppressWarnings({ "unchecked", "rawtypes" })
List<String>scops = new <String>ArrayList();
scops.add("https://www.googleapis.com/auth/youtubepartner");
final HttpTransport TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
// Create a listener for automatic refresh OAuthAccessToken
List<CredentialRefreshListener> list = new ArrayList<CredentialRefreshListener>();
list.add(new CredentialRefreshListener() {
@Override
public void onTokenResponse(Credential credential,
TokenResponse tokenResponse) throws IOException {
System.out.println(tokenResponse.toPrettyString());
}
public void onTokenErrorResponse(Credential credential,
TokenErrorResponse tokenErrorResponse)
throws IOException {
System.err.println("Error: "
+ tokenErrorResponse.toPrettyString());
}
});
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(EmailId)
.setServiceAccountScopes(scops)
.setServiceAccountPrivateKeyFromP12File(new File("test.p12"))
.setRefreshListeners(list)
.build();
credential.refreshToken();
第 2 步:在步骤 1 中获取的访问令牌作为参数在下面提到的 URL 中传递以获取 Youtube 视频 ID
示例网址为:
https://www.googleapis.com/youtube/partner/v1/claimSearch?assetId=xxxxxxxxx&onBehalfOfContentOwner=xxxxxxxx&status=active&access_token=ya29.-gCmzBHciDghrj2EDtBn1Vx0MV38pNLZTvqfwOyG0hNJCj75nsCBA5zaxmP1sr7UqI7ZrYI3AIZstA我在步骤 403 中收到错误代码 2。
{ "error": { "errors": [ { "domain": "usageLimits", "reason": "accessNotConfigured", "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration.", "extendedHelp": "https://console.developers.google.com" } ], "code": 403, "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration." } }
已经启用了"GOOGLE CAN JSON API"和"YOUTUBE DATA API"。任何人都可以帮我解决这个问题吗?
我从以前的项目中知道您需要在 Google 控制台中启用 API。我会先尝试一下。
https://code.google.com/apis/console
您尝试使用 Youtube 的 Content ID API 端点/youtube/partner/v1/claimSearch。
此端点不是 Youtube 数据 API 的一部分,只有当您参与"Youtube 合作伙伴计划"时,才能访问它。否则,它甚至不会在开发人员控制台中列出。
更多信息请见:https://developers.google.com/youtube/partner/
注意:YouTube 内容 ID API 仅供 YouTube 内容合作伙伴使用,并非所有开发者或所有 YouTube 用户都可以访问。如果您没有看到 YouTube Content ID API 是 Google 开发者控制台中列出的服务之一,请参阅 www.youtube.com/partner 以详细了解 YouTube 合作伙伴计划。