YouTube API Auth 类中的 Nullpointer 异常 - 使用 Java 上传视频
Posted
技术标签:
【中文标题】YouTube API Auth 类中的 Nullpointer 异常 - 使用 Java 上传视频【英文标题】:Nullpointer Exception in YouTube API Auth class - Video Upload using Java 【发布时间】:2016-07-26 19:53:40 【问题描述】:我想使用 Java 在 YouTube 上上传视频。 一切正常,但是在应该读取用于身份验证的 secrets-JSON 时,我得到了 Nullpointer 异常。
有人可以帮帮我吗?
Throwable: null
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:89)
at java.io.InputStreamReader.<init>(InputStreamReader.java:85)
at Auth.authorize(Auth.java:66)
at Upload.<init>(Upload.java:95)
at Main.main(Main.java:30)
这是代码:
public class Auth
/**
* Define a global instance of the HTTP transport.
*/
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/**
* Define a global instance of the JSON factory.
*/
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
/**
* This is the directory that will be used under the user's home directory where OAuth tokens will be stored.
*/
private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials";
/**
* Authorizes the installed application to access user's protected data.
*
* @param scopes list of scopes needed to run youtube upload.
* @param credentialDatastore name of the credential datastore to cache OAuth tokens
*/
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException
// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
// Checks that the defaults have been replaced (Default = "Enter X here").
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter "))
System.out.println(
"Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/credential "
+ "into src/main/resources/client_secrets.json");
System.exit(1);
// This creates the credentials datastore at ~/.oauth-credentials/$credentialDatastore
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore)
.build();
// Build the local server and bind it to port 8080
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
这是我得到 Nullpointer Exeption 的那一行:
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
非常感谢您的帮助。 我希望我能尽快解决这个问题。
【问题讨论】:
【参考方案1】:所以这里有一个解决方案,如果你想使用json或视频的文件路径:
//Load client secrets
URI filePath = new URI (path);
Reader clientSecretReader = new InputStreamReader(new FileInputStream (filePath.toString()));
【讨论】:
如果你之后又要去 .toString 它,为什么还要麻烦把它放到一个 URI 中呢?不妨把路径直接放到FileInputStream的参数中【参考方案2】:对于 getResourceAsStream 文档:
返回:一个 URL 对象,如果没有找到同名的资源,则返回 null
您确定存在名为“/client_secrets.json”的资源吗?如果您想指定相对路径,也许反斜杠不应该出现在名称中。
【讨论】:
当然! :DI 也尝试给出文件的直接位置,但效果不佳... Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/VideoUpload_YouTube/resources/client_secrets.json") ); 我在 spring mvc 应用程序中尝试了类似的代码,没有反斜杠,适用于放置在我的 src/main/resources 文件夹中的资源的属性(预计资源所在的位置)。我认为您应该尝试不使用反斜杠并验证资源是否位于 ClassLoader 期望找到它的同一路径中。 好的。谢谢!你说的对。当它在这个目录中时,它可以工作。有没有其他可能发送文件的路径? 我不确定,但我认为您不能订购完整的绝对路径。如文档中所述:“查找具有给定名称的资源。资源是可以通过类代码以独立于代码位置的方式访问的一些数据(图像、音频、文本等)。”。所以我假设你可以使用这个目录。 好的。感谢您的帮助!以上是关于YouTube API Auth 类中的 Nullpointer 异常 - 使用 Java 上传视频的主要内容,如果未能解决你的问题,请参考以下文章
喜欢使用 YouTube 数据 API v3 在 YouTube 上使用访问令牌的视频吗?
Auth0 登录调用 Youtube Data API v3
如何在 Android 中使用 youtube 数据 API 搜索视频