我甚至设置了对YouTube数据API V3的最基本访问权。问题的一部分是在API的Java QuickStart页面上进行测试与使用实际Java程序中生成的代码之间的不一致之处。
我可以按照以下说明https://developers.google.com/youtube/v3/quickstart/java并在https://develovelers.google.com/youtube/v3/docs/channels/list?apix_params =; params={"PART":"SNIPPET,CONTENTEDETDETELSS;22%3A%22UC_X5XG1OV2P6UZZ5FSM9TTW%22%7D用于使用通道ID列出通道内容的网页。
问题是,当我将代码复制到适当位置的Gradle项目时,我会遇到403错误。所使用的代码如下,我的json client_secrets文件在我的src/main/resources文件夹中。
public class ApiExample {
private static final String CLIENT_SECRETS= "client_secrets.json";
private static final Collection<String> SCOPES =
Arrays.asList("https://www.googleapis.com/auth/youtube.readonly");
private static final String APPLICATION_NAME = "API code samples";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
InputStream in = ApiExample.class.getResourceAsStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.build();
Credential credential =
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
return credential;
}
/**
* Build and return an authorized API client service.
*
* @return an authorized API client service
* @throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
/**
* Call function to create API service object. Define and
* execute API request. Print API response.
*
* @throws GeneralSecurityException, IOException, GoogleJsonResponseException
*/
public static void main(String[] args)
throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();
// Define and execute the API request
YouTube.Channels.List request = youtubeService.channels()
.list("snippet,contentDetails,statistics");
ChannelListResponse response = request.setId("UC_x5XG1OV2P6uZZ5FSM9Ttw").execute();
System.out.println(response);
}
}
我希望我从API示例页面上获得的HTTP 200响应,并列出了适当的JSON。运行实际的Java应用程序时的问题是403错误。
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Access Not Configured. YouTube Data API has not been used in project 113360591925 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=113360591925 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
"reason" : "accessNotConfigured",
"extendedHelp" : "https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=113360591925"
} ],
"message" : "Access Not Configured. YouTube Data API has not been used in project 113360591925 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=113360591925 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at ApiExample.main(ApiExample.java:82)
我已经设置了适当的范围,但是我尚未完成验证步骤,因为这是一个内部应用程序,每周最多可以触摸YouTube(平均一次(。我设置的范围在此处显示https://www.dropbox.com/s/74owbu9589v4xrl/capture.png?dl = 0,今天使用YouTube数据API启用了客户秘密。
我需要验证吗,我的假设是我没有给出工作示例。我没有提供的域或隐私政策。我缺少什么吗?
在进行了更多的研究和进一步的测试之后,我在stackoverflow上找到了一个问题答案,这使我创建了一个新项目。
仅使用我上传所需的范围,这有效。我不知道为什么,而且似乎没有其他人。这是答案https://stackoverflow.com/a/544456853/2503583
授予我没有执行API密钥步骤,而是做了我的OAuth凭据,因为那是我所需要的。