gpt4 book ai didi

asp.net - ASP.NET 中 Google API 中的 redirect_uri_mismatch

转载 作者:行者123 更新时间:2023-12-02 05:48:54 27 4
gpt4 key购买 nike

我正在尝试使用 ASP.NET Web 窗体将视频上传到我的 YouTube channel 。我创建了开发者帐户和 tested it working using JavaScript based solution每次都需要登录才能上传视频。

我希望我网站的用户直接在我的 channel 上上传视频,并且每个身份验证都应该在代码后面,不应提示用户登录。为此,我编写了以下类(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;
}
}
}

现在将此类用于 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 Developer Console 上使用了它们,如下所示:

enter image description here

运行时 http://localhost:29540/default.aspx打开一个显示“redirect_uri_mismatch”错误的新选项卡,如下所示:

enter image description here

此时如果我查看浏览器地址,我会看到 redirect_uri 设置为 http://localhost:37294/authorize我只是手动将其更改为 http://localhost:29540/default.aspx生成 token 。

那么,您能否建议在上面的代码中进行更改的位置,以便从我的应用程序端正确填写请求 uri。

最佳答案

浪费了一天时间,我才知道下面的重定向 URL 适用于所有本地主机 Web 应用程序。因此,您需要在 Google 开发者控制台网络应用程序的“授权重定向 URI”上使用以下 URL。

http://localhost/authorize/

关于asp.net - ASP.NET 中 Google API 中的 redirect_uri_mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33228300/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com