public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
InputStream in = new FileInputStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.setAccessType("offline")
.build();
int defaultPort = 5005;
LocalServerReceiver customReceiver = new LocalServerReceiver.Builder()
.setHost("localhost")
.setPort(defaultPort)
.build();
Credential credential = new AuthorizationCodeInstalledApp(flow, customReceiver).authorize("userid");
return credential;
}
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Credential credential = authorize(httpTransport);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
So Everything works for me but i need to login everytime which I would prefer not doing. Is it possible to modify the code or anything else so it does that automaticly with given Email and password?
The client library will handle user token storage if its configured properly.
You forgot to configure setDataStoreFactory
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
My TOKENS_DIRECTORY_PATH is just set to tokens then it stores them in the current directory.
private static final String TOKENS_DIRECTORY_PATH = "tokens";