YouTube API Auth类中的Nullpointer异常 - 使用Java

时间:2016-04-06 12:07:22

标签: java json authentication youtube youtube-api

我想使用Java在YouTube上上传视频。 一切正常,但在应该读取秘密-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"));

非常感谢你的帮助。 我希望我能尽快解决这个问题。

2 个答案:

答案 0 :(得分:0)

对于getResourceAsStream文档:

返回:一个URL对象,如果没有找到具有此名称的资源,则返回null

您确定资源名为&#34; /client_secrets.json"存在?如果你想指定相对路径,也许反斜杠不应该在名称中。

答案 1 :(得分:0)

所以这是一个解决方案,如果你想使用json或视频的文件路径:

//Load client secrets 
URI filePath = new URI (path);      
Reader clientSecretReader = new InputStreamReader(new FileInputStream (filePath.toString()));