I'm trying to create a java app that runs command line, from a shell script, in a cron job. That being the case, I won't have a user present to agree to allow access in a browser. This application will query a directory, and upload videos in this directory to YouTube. I'm new to the YouTube api and to Oauth2.0 as well. In all the research I've done, I see much conflicting information about how to accomplish what I'm trying to do.
Here's my code. Please let me know where you see me going wrong. Thanks in advance!
public static Credential authorize(List scopes, String credentialDatastore) throws IOException {
// Load client secrets.
//Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/main/resources/client_secrets.json"));
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/main/resources/client_secret_file.apps.googleusercontent.com.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}
// THIS IS WHERE I NEED TO CHANGE, TO WORK WITH THIS APP!!!!
// FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File("." + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).setAccessType("offline")
.build();
// Build the local server and bind it to port 8080
// LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
// Authorize.
try {
Credential returnCreds = new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("xxxxxxxxxxxxxxxxxxx");
returnCreds.refreshToken();
return returnCreds;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if not authorized, will return a null credential
return null;
}