我正在创建一个springboot和react应用程序。我想上传视频到youtube使用youtube数据api v3。我创建了oauth凭据,如下所示,但它不会给我正确的重定向uri。相反,它为我提供了默认的重定向uri。如果我将oauth创建为桌面应用程序,那么它工作得非常好。在web应用程序中,它将不起作用。我做错什么了?
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();
}
暂无答案!
目前还没有任何答案,快来回答吧!