尝试访问 youtube 数据 api 时,在浏览器中弹出 google oauth 屏幕

问题描述

我使用前端 react js 和后端 spring boot。当我尝试通过 youtube 数据 api 将视频上传到 youtube 时,需要 oauth 访问。该 url 显示在 spring boot 控制台中。直到视频上传成功,前端才会有请求。我如何才能将该请求发送到前端并在浏览器中显示为弹出窗口。

这是我的代码。(在 spring boot 控制器中)

    private static final Collection<String> ScopES = Arrays.asList("https://www.googleapis.com/auth/youtube.upload");

    private static final String APPLICATION_NAME = "API code samples";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    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,credential).setApplicationName(APPLICATION_NAME)
                .build();
    }


    @GetMapping
    public void upload() throws GeneralSecurityException,IOException,GoogleJsonResponseException {
        YouTube youtubeService = getService();
        
        Video video = new Video();

        // Add the snippet object property to the Video object.
        VideoSnippet snippet = new VideoSnippet();
        snippet.setTitle("test");
        snippet.setDescription("description for testing purpose");
        video.setSnippet(snippet);
        
        // Add the status object property to the Video object.
        VideoStatus status = new VideoStatus();
        status.setUploadStatus("uploaded");
        video.setStatus(status);

        Resource load = storageService.load("video 3.mp4","01","1hruc8cc0gshe");//this returns the video file as resource
        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();
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)