我正在开发 Java 代码,通过使用服务帐户身份验证来检索相应 Assets ID 的视频 ID。我已按照下面提到的步骤进行操作。 步骤 1:编写一个 Java 程序,使用服务帐户从 Google 身份验证中检索访问 token 。 该代码用于获取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 步中获取的访问 token 作为参数传递到下面提到的 URL 中,以获取 Youtube 视频 ID
示例网址为:
-
{ "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 COULD JSON API”和“YOUTUBE DATA API”。 谁能帮我解决这个问题吗?
最佳答案
通过之前的项目,我知道您需要在 Google 控制台中启用 API。我会先尝试一下。
关于java - 使用服务帐户的 Youtube 视频 ID - 收到错误 403 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27924617/