Mandy Fischer Mandy Fischer - 5 months ago 34
Java Question

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
at Auth.authorize(
at Upload.<init>(
at Main.main(

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 ")) {
"Enter Client ID and Secret from "
+ "into src/main/resources/client_secrets.json");

// 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 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.


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()));