I am creating a Spring Boot and React application. I want to upload videos to YouTube using the YouTube Data API v3. I created OAuth credentials as below, but it won't give me correct redirect uri. Instead of that it gives me the default redirect uri. If I create OAuth as desktop application it worked perfectly. In web application it won't work. What am I doing wrong?
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
InputStream in = YoutubeController.class.getResourceAsStream(CLIENT_SECRETS);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY,
clientSecrets, SCOPES).build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
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();
}
@GetMapping("/{uvid}")
public void upload(@PathVariable(value = "uvid") String uvid) throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();
Video video = new Video();
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle("aaaaaaaaaaaaa");
snippet.setDescription("description for testing purpose");
video.setSnippet(snippet);
VideoStatus status = new VideoStatus();
status.setUploadStatus("uploaded");
video.setStatus(status);
Resource load = storageService.load("video 3.mp4", "70", "1hruc8cc0gshe");//this returns image as a resource file
File myfile = load.getFile();
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream",
new BufferedInputStream(new FileInputStream(myfile)));
mediaContent.setLength(myfile.length());
// Define and execute the API request
YouTube.Videos.Insert request = youtubeService.videos().insert("snippet, status", video, mediaContent);
Video response = request.execute();
}