我在https://developers.google.com/youtube/v3/quickstart/java上遵循了Google针对YouTube数据API的快速入门
我拿到client_secret.json
并将其正确安装在我的项目中。在他们的authorize
方法中使用他们的基本代码似乎给我带来了麻烦。
private static HttpTransport HTTP_TRANSPORT;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final Collection<String> SCOPES = Arrays.asList("YouTubeScopes.https://www.googleapis.com/auth/youtube.force-ssl YouTubeScopes.https://www.googleapis.com/auth/youtubepartner");
public static Credential authorize() throws IOException
{
InputStream in = Registering.class.getResourceAsStream("client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = null;
try {
credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
} catch (Exception e) {
e.printStackTrace();
}
return credential;
}
这只是给我带来问题的摘要,我拥有的所有其他内容几乎与他们提供的Quickstart文件完全相同。
运行此代码时,我总是在这行上得到NullPointerException
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
我已经进行了两次和三次检查,以确保它正确加载了client_secret.json
并且位于InputStream中。
为什么会发生这种情况?修复此代码引发此异常的方法是什么?