0

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?

enter image description here

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();
    }
dilanka
  • 31
  • 5
  • Please provide a [mre]. – stvar Mar 18 '21 at 08:48
  • i update my question with code. – dilanka Mar 18 '21 at 09:05
  • The OAuth 2 flow code you've shown above is working *only* for installed apps. In case of web applications, you have to use a different logic: see a very short description of it [here](https://stackoverflow.com/a/65786705/8327971). I recommend to follow [@DaImTo](https://stackoverflow.com/users/1841839/daimto)'s answer: [Authorization working locally but not hosted](https://stackoverflow.com/a/54920273/8327971). – stvar Mar 18 '21 at 13:32
  • I followed two urls that you given to me. But i still not understand how to apply these codes to adapt to my situation. can you give me little help to understand that – dilanka Mar 19 '21 at 04:49
  • that example is based on CalendarServletSample. how can i handle that. i dont want to use any calender information – dilanka Mar 19 '21 at 04:57
  • [Here](https://github.com/google/google-api-java-client-samples/tree/master/calendar-appengine-sample) is the complete *Calendar* sample project from Google. Read it, understand how it works, run it eventually. Then adapt this code to your context. The OAuth flow for web applications of this sample code functions the very same way in all other contexts of web apps needing OAuth authentication/authorization. – stvar Mar 19 '21 at 07:49

0 Answers0