ASP.NET中Google API中的重定向uri不匹配
我正在尝试使用ASP.NET Web表单在我的YouTube频道上传视频。我创建了开发者帐户,每次上传视频都需要登录 我希望我的网站的用户上传视频直接在我的频道和每个认证应该在代码背后,用户不应该被提示登录。为此,我编写了以下课程:ASP.NET中Google API中的重定向uri不匹配,asp.net,asp.net-mvc,google-api,google-api-client,google-api-dotnet-client,Asp.net,Asp.net Mvc,Google Api,Google Api Client,Google Api Dotnet Client,我正在尝试使用ASP.NET Web表单在我的YouTube频道上传视频。我创建了开发者帐户,每次上传视频都需要登录 我希望我的网站的用户上传视频直接在我的频道和每个认证应该在代码背后,用户不应该被提示登录。为此,我编写了以下课程: public class UploadVideo { public async Task Run(string filePath) { string CLIENT_ID = "1111111111111111111111.apps.g
public class UploadVideo
{
public async Task Run(string filePath)
{
string CLIENT_ID = "1111111111111111111111.apps.googleusercontent.com";
string CLIENT_SECRET = "234JEjkwkdfh1111";
var youtubeService = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "SingleUser");
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"
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();
}
}
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;
}
}
void videosInsertRequest_ResponseReceived(Video video)
{
Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
}
public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { YouTubeService.Scope.Youtube, // view and manage your YouTube account
YouTubeService.Scope.YoutubeForceSsl,
YouTubeService.Scope.Youtubepartner,
YouTubeService.Scope.YoutubepartnerChannelAudit,
YouTubeService.Scope.YoutubeReadonly,
YouTubeService.Scope.YoutubeUpload};
try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "YouTube Data API Sample",
});
return service;
}
catch (Exception ex)
{
//Console.WriteLine(ex.InnerException);
return null;
}
}
}
现在将该类用于default.aspx的Page_Load(),如下所示:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string path = "C:/Users/abhi/Desktop/TestClip.mp4";
new UploadVideo().Run(path).Wait();
}
catch (AggregateException ex)
{
//catch exceptions
}
}
当我运行这个(default.aspx)页面时,我看到旋转,所以我在Google开发者控制台上使用它们,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string path = "C:/Users/abhi/Desktop/TestClip.mp4";
new UploadVideo().Run(path).Wait();
}
catch (AggregateException ex)
{
//catch exceptions
}
}
运行时打开一个新选项卡,其中显示“重定向\u uri\u不匹配”错误,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string path = "C:/Users/abhi/Desktop/TestClip.mp4";
new UploadVideo().Run(path).Wait();
}
catch (AggregateException ex)
{
//catch exceptions
}
}
在这一点上,如果我查看浏览器地址,我会看到redirect_uri设置为,我只是手动将其更改为生成令牌
所以,您能否建议在上面的代码中进行更改,以便从我的应用程序端正确填写请求uri。一天之后,我知道下面的重定向URL适用于所有localhost web应用程序。所以您需要在google开发者控制台web应用程序的“授权重定向URI”上使用下面的URL
谢谢你,我意识到有时候你必须在URL的末尾加“/”而其他时候则不行。因为这种愚蠢,我浪费了几个小时。