我创建了此HTTP触发式天蓝色函数,该函数包含(以下)用于自动将视频上传到YouTube的代码。来源:(https://developers.google.com/youtube/v3/docs/videos/insert)。
我还使用Google Console API创建了Client_Id和Client_secret,并将这些数据存储在client_secrets.json文件中。 (例如https://github.com/youtube/api-samples/blob/master/dotnet/client_secrets.json)
我尝试在本地运行该函数,并将其粘贴:http://localhost:7071/api/Function1在浏览器中,出现以下错误:
400,这是一个错误。错误:redirect_rui_mismatch中的重定向URI请求http://localhost58085/authorize/与请求不匹配授权给OAuth客户端。更新授权的重定向URI,请访问。 http://consolse.Developers.google.com/apis/credentials。
我不确定我在做什么错,而且我不知道应该为“授权重定向URI”输入什么URL
代码:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3.Data;
using System.Reflection;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using System.Threading;
namespace UploadVideo
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogInformation("YouTube Data API: Upload Video");
log.LogInformation("==============================");
try
{
await Run();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
log.LogInformation("Error: " + e.Message);
}
}
return new OkObjectResult($"Video Processed..");
}
private static async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
var filePath = @"C:\Users\Peter\Desktop\audio\Test.mp4"; // Replace with path to actual movie file.
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
}
private static void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
Console.WriteLine("{0} bytes sent.", progress.BytesSent);
break;
case UploadStatus.Failed:
Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
break;
}
}
private static void videosInsertRequest_ResponseReceived(Video video)
{
Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
}
}
}
Oauth2的工作方式是向用户显示同意屏幕
您是否要授予Peters出色的应用程序访问您的YouTube帐户的权限。
如果用户接受此命令,则在重定向uri时将授权代码返回到您的应用程序
这是您的应用程序中能够处理授权握手的文件。此重定向uri必须在Google开发人员控制台中注册,这有助于确保没有人试图劫持您的授权呼叫并将其发送到他们的网站,然后他们才可以访问用户数据。
[请记住,GoogleWebAuthorizationBroker.AuthorizeAsync用于已安装的应用程序,如果您尝试将其设置为功能,它将在服务器上打开浏览器同意窗口。您需要为Web应用程序执行更多此类操作Asp.net mvc,我不能声称对Azure函数有很多了解,但是如果有类似云功能的内容,我认为它们没有能力向您的用户显示Web浏览器页面。我认为您无法通过azure函数使用Oauth2对用户进行身份验证。