Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullpointer Exception in YouTube API Auth class - Video Upload using Java

I want to upload a video on YouTube using Java. Everything works fine, but at the point where the secrets-JSON for authentication should be read, I get a Nullpointer Exception.

Can please someone help me?

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)

This is the code:

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");
}
}

And this is the line where I get the Nullpointer Exeption:

Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));

Thanks a lot for your help. I hope I can fix this soon.


1 Answers

So here is a solution, if you want to use the filepath of the json or the video:

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!