ASP.NET中Google API中的redirect_uri_mismatch

gxwragnw  于 2023-11-20  发布在  .NET
关注(0)|答案(3)|浏览(140)

我试图上传视频在我的YouTube频道使用ASP.NET Web窗体.我创建了开发者帐户和tested it working using JavaScript based solution,需要登录每次上传视频.
我希望我的网站的用户上传视频直接在我的频道和每一个身份验证应该在代码后面,用户不应该被提示登录.为此,我写了下面的类:

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
    }
}


当我运行这个(默认.aspx)页面时,我看到了http://localhost:29540/default.aspx旋转,所以我在Google Developer Console上使用了它们,如下所示:


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



此时,如果我查看浏览器地址,我会看到redirect_uri设置为http://localhost:37294/authorize,我只需手动将其更改为http://localhost:29540/default.aspx,这会生成一个令牌。
所以,你能建议在上面的代码中进行修改,以便从我的应用程序端正确填充请求URI吗?

4ktjp1zp

4ktjp1zp1#

浪费了一天,然后我才知道下面的重定向URL适用于所有本地主机Web应用程序。所以你需要在谷歌开发者控制台Web应用程序的“授权重定向URI”上使用下面的URL。

http://localhost/authorize/

字符串

7gcisfzg

7gcisfzg2#

对于任何在2022年仍然有这个问题的人,我想出了一个解决方案。如果你使用https://localhost:portnumber作为你的重定向URI,就使用作为你的重定向URI。它最终为我工作

qmelpv7a

qmelpv7a3#

尝试配置https://localhost:7050/signin-google使用您的端口号在您的API控制台在谷歌

相关问题