redirect_uri_mismatch 在 Google API 在 ASP.NET
redirect_uri_mismatch in Google APIs in ASP.NET
我正在尝试使用 ASP.NET 网络表单将视频上传到我的 YouTube 频道。我创建了开发者帐户,tested it working using JavaScript based solution 每次都需要登录才能上传视频。
我希望我网站的用户直接在我的频道上上传视频,并且每个身份验证都应该在代码后面,不应提示用户登录。为此,我写了以下 class:
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;
}
}
}
现在将此 class 用于 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) 页面时,我看到 http://localhost:29540/default.aspx 旋转,所以我在 Google 开发者控制台上使用了它们,如下所示:
在 运行 宁 http://localhost:29540/default.aspx 打开一个新标签页,其中显示 "redirect_uri_mismatch" 错误,如下所示:
此时如果我查看浏览器地址,我会看到 redirect_uri 设置为 http://localhost:37294/authorize 我只是手动更改它到 http://localhost:29540/default.aspx 生成一个令牌。
那么,你能否建议在上面的代码中进行更改,以便从我的应用程序端正确填充请求 uri。
浪费了一天时间,我才知道下面的重定向 URL 适用于所有本地主机 Web 应用程序。因此,您需要在 google 开发人员控制台 Web 应用程序的 "Authorized redirect URIs".
上使用以下 URL
http://localhost/authorize/
对于 2022 年仍然遇到此问题的任何人,我找到了解决方案。如果您使用 https://localhost:portnumb 作为您的重定向 uri,只需使用 https://127.0.0.1:sameportnumb 作为您的重定向 uri。它最终对我有用
我正在尝试使用 ASP.NET 网络表单将视频上传到我的 YouTube 频道。我创建了开发者帐户,tested it working using JavaScript based solution 每次都需要登录才能上传视频。
我希望我网站的用户直接在我的频道上上传视频,并且每个身份验证都应该在代码后面,不应提示用户登录。为此,我写了以下 class:
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;
}
}
}
现在将此 class 用于 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) 页面时,我看到 http://localhost:29540/default.aspx 旋转,所以我在 Google 开发者控制台上使用了它们,如下所示:
在 运行 宁 http://localhost:29540/default.aspx 打开一个新标签页,其中显示 "redirect_uri_mismatch" 错误,如下所示:
此时如果我查看浏览器地址,我会看到 redirect_uri 设置为 http://localhost:37294/authorize 我只是手动更改它到 http://localhost:29540/default.aspx 生成一个令牌。
那么,你能否建议在上面的代码中进行更改,以便从我的应用程序端正确填充请求 uri。
浪费了一天时间,我才知道下面的重定向 URL 适用于所有本地主机 Web 应用程序。因此,您需要在 google 开发人员控制台 Web 应用程序的 "Authorized redirect URIs".
上使用以下 URLhttp://localhost/authorize/
对于 2022 年仍然遇到此问题的任何人,我找到了解决方案。如果您使用 https://localhost:portnumb 作为您的重定向 uri,只需使用 https://127.0.0.1:sameportnumb 作为您的重定向 uri。它最终对我有用