这真是太疯狂了,我花了一个星期的时间试图解决这个问题。我找到的所有东西要么被弃用,要么就行不通。
所以这就是我想要做的。我们有用户上传视频,我们会将视频存储到批准之前。一旦获得批准,我们需要将其上传到我们的YouTube频道。
来自Google:https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads的示例将无法通过GoogleWebAuthorizationBroker.AuthorizeAsync,因为它只会永久挂起。
这种方法的另一个问题是我们在上传视频后需要ID,我们需要知道视频是否成功上传,全部同步。你会看到它使用异步方法的代码,并获得视频的id有一个回调。
有人知道如何同步在mvc应用程序的后端上传视频吗?
答案 0 :(得分:10)
好的,我遇到的第一个问题是身份验证挂起(从GoogleWebAuthorizationBroker.AuthorizeAsync获取凭据)。解决这个问题的方法是使用GoogleAuthorizationCodeFlow,它不是异步的,并且不会尝试在appdata文件夹中保存任何内容。
我需要获得一个刷新令牌,为此我遵循: Youtube API single-user scenario with OAuth (uploading videos)
要获得可以多次使用的刷新令牌,您必须为安装的应用程序获取客户端ID和密码。
凭据是艰难的部分,之后事情就好了。有一点需要注意,因为我花了几个小时试图找出上传视频时的CategoryId。我似乎无法找到任何关于示例代码到达“22”的真实解释。我发现22是默认值,意思是“人与博客”。
下载我需要的任何人的代码(我还需要能够删除youtube视频,所以我在这里添加了):
public class YouTubeUtilities
{
/*
Instructions to get refresh token:
* https://stackoverflow.com/questions/5850287/youtube-api-single-user-scenario-with-oauth-uploading-videos/8876027#8876027
*
* When getting client_id and client_secret, use installed application, other (this will make the token a long term token)
*/
private String CLIENT_ID {get;set;}
private String CLIENT_SECRET { get; set; }
private String REFRESH_TOKEN { get; set; }
private String UploadedVideoId { get; set; }
private YouTubeService youtube;
public YouTubeUtilities(String refresh_token, String client_secret, String client_id)
{
CLIENT_ID = client_id;
CLIENT_SECRET = client_secret;
REFRESH_TOKEN = refresh_token;
youtube = BuildService();
}
private YouTubeService BuildService()
{
ClientSecrets secrets = new ClientSecrets()
{
ClientId = CLIENT_ID,
ClientSecret = CLIENT_SECRET
};
var token = new TokenResponse { RefreshToken = REFRESH_TOKEN };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
}),
"user",
token);
var service = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = "TestProject"
});
//service.HttpClient.Timeout = TimeSpan.FromSeconds(360); // Choose a timeout to your liking
return service;
}
public String UploadVideo(Stream stream, String title, String desc, String[] tags, String categoryId, Boolean isPublic)
{
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = title;
video.Snippet.Description = desc;
video.Snippet.Tags = tags;
video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = isPublic ? "public" : "private"; // "private" or "public" or unlisted
//var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
videosInsertRequest.ProgressChanged += insertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += insertRequest_ResponseReceived;
videosInsertRequest.Upload();
return UploadedVideoId;
}
public void DeleteVideo(String videoId)
{
var videoDeleteRequest = youtube.Videos.Delete(videoId);
videoDeleteRequest.Execute();
}
void insertRequest_ResponseReceived(Video video)
{
UploadedVideoId = video.Id;
// video.ID gives you the ID of the Youtube video.
// you can access the video from
// http://www.youtube.com/watch?v={video.ID}
}
void insertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
// You can handle several status messages here.
switch (progress.Status)
{
case UploadStatus.Failed:
UploadedVideoId = "FAILED";
break;
case UploadStatus.Completed:
break;
default:
break;
}
}
}
我还没有尝试过,但据我所知,ApplicatioName可以是你想要的任何东西。我只是在测试,这是我在youtube中为客户端ID和秘密提供的项目名称,但我认为你可以放任何东西?