ASP.NET中的Google API中的redirect_uri_mismatch

问题描述 投票:2回答:1

我正在尝试使用ASP.NET Web Form在我的YouTube频道上传视频。我创建了开发者帐户和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
    }
}

当我运行这个(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

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

asp.net asp.net-mvc google-api google-api-client google-api-dotnet-client
1个回答
8
投票

一天浪费然后我知道下面重定向URL适用于所有localhost Web应用程序。因此,您需要在google开发人员控制台Web应用程序的“授权重定向URI”上使用以下URL。

http://localhost/authorize/
© www.soinside.com 2019 - 2024. All rights reserved.