C# 如何解决ASP.NET MVC中的重定向uri不匹配问题?

C# 如何解决ASP.NET MVC中的重定向uri不匹配问题?,c#,asp.net-mvc,google-oauth,google-calendar-api,google-api-dotnet-client,C#,Asp.net Mvc,Google Oauth,Google Calendar Api,Google Api Dotnet Client,我已经创建了ASP.NET MVC web应用程序来与google日历API集成 我关注了谷歌网站上的文档。我在Google开发者控制台中设置了该项目,如下所示: 创建项目后,我还下载了凭据文件,但在使用它时,每次都会出现uri\u不匹配错误 如何解决这个问题 我的授权代码是: string jsonFile = HttpContext.Server.MapPath("~/credentials.json"); UserCredential cr

我已经创建了ASP.NET MVC web应用程序来与google日历API集成

我关注了谷歌网站上的文档。我在Google开发者控制台中设置了该项目,如下所示:

创建项目后,我还下载了凭据文件,但在使用它时,每次都会出现
uri\u不匹配
错误

如何解决这个问题

我的授权代码是:

string jsonFile = HttpContext.Server.MapPath("~/credentials.json");

            UserCredential credential;

            using (var stream =
                new FileStream(jsonFile, FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                //string credPath = "token.json";
                string credPath = HttpContext.Server.MapPath("~/token");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define parameters of request.
            EventsResource.ListRequest request = service.Events.List("primary");
            request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 10;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;


您在Google开发者控制台中设置的重定向uri必须与您从何处发送并期望返回响应完全匹配

我将假设您正在使用Google.net客户端库,因为您的响应显示/authorize,该库中配置了/authorize

uri_不匹配的解决方案 在Google开发者控制台中添加

http://localhost:63972/authorize

但是请确保在IDE()中设置了静态端口,否则会再次出现相同的问题

提示web应用程序与已安装的应用程序 您发布的代码使用了
GoogleWebAuthorizationBroker.AuthorizationAsync
,它专为安装的应用程序而设计。当授权窗口打开时,它将在运行代码的机器上打开。当您在开发中时,它将在您的开发机器上打开,但当您尝试将其发布到服务器时,它将不起作用

下面的示例应该使用
GoogleAuthorizationCodeFlow


您在Google开发者控制台中设置的重定向uri必须与您从何处发送并期望返回响应完全匹配

我将假设您正在使用Google.net客户端库,因为您的响应显示/authorize,该库中配置了/authorize

uri_不匹配的解决方案 在Google开发者控制台中添加

http://localhost:63972/authorize

但是请确保在IDE()中设置了静态端口,否则会再次出现相同的问题

提示web应用程序与已安装的应用程序 您发布的代码使用了
GoogleWebAuthorizationBroker.AuthorizationAsync
,它专为安装的应用程序而设计。当授权窗口打开时,它将在运行代码的机器上打开。当您在开发中时,它将在您的开发机器上打开,但当您尝试将其发布到服务器时,它将不起作用

下面的示例应该使用
GoogleAuthorizationCodeFlow


这种不匹配不是很明显吗
127.0.0.1
不是
localhost
。而且端口也不同。想必您在某种程度上误解了它,所以它在一个位置使用localhost,在另一个位置使用127.x。当然,我们不能在没有看到更多细节的情况下明确告诉您出了什么问题。@ADyson我可以从哪里更改塔尔?这可能只是您在Visual Studio中的启动设置,意味着应用程序正在以不同的方式运行setup@ADyson这个问题的作者并没有配置它,它是由google.net客户端自动完成的库。@BirazDahal除非这是一个已安装的应用程序,其代码不能在web服务器上运行,请参阅下面的答案。不匹配不是很明显吗
127.0.0.1
不是
localhost
。而且端口也不同。想必您在某种程度上误解了它,所以它在一个位置使用localhost,在另一个位置使用127.x。当然,我们不能在没有看到更多细节的情况下明确告诉您出了什么问题。@ADyson我可以从哪里更改塔尔?这可能只是您在Visual Studio中的启动设置,意味着应用程序正在以不同的方式运行setup@ADyson这个问题的作者并没有配置它,它是由google.net客户端自动完成的库。@BirazDahal除非这是一个已安装的应用程序,其代码无法在web服务器上运行,请参阅下面的答案。@DalmTo我尝试过这个过程,ubt我得到的结果。凭据始终为null。var result=wait new AuthorizationCodeMvcApp(这是新的AppFlowMetadata())。AuthorizeAsync(取消令牌);我输入了上面的代码来获取凭据,并且总是得到null。在谷歌API控制台上还有什么需要做的吗?我已经添加了授权重定向文件,还生成了客户端ID和客户端机密ID如果您对GoogleAuthorizationCodeFlow有问题,我将用您的新代码提出一个新问题。因为它与你在这里发布的问题无关。谢谢!!!!!!它最终也在服务器上工作。但是,每次我在不同的浏览器中打开gmail账户时,我都会遇到一个问题。有没有办法只登录一次帐户,因为我需要用我正在登录的电子邮件在我的客户端谷歌日历中设置事件。@DalmTo我尝试过这个过程,ubt我得到的result.credentials始终为null。var result=wait new AuthorizationCodeMvcApp(这是新的AppFlowMetadata())。AuthorizeAsync(取消令牌);我输入了上面的代码来获取凭据,并且总是得到null。在谷歌API控制台上还有什么需要做的吗?我已经添加了授权重定向文件,还生成了客户端ID和客户端机密ID如果您对GoogleAuthorizationCodeFlow有问题,我将用您的新代码提出一个新问题。因为它与你在这里发布的问题无关。谢谢!!!!!!它最终也在服务器上工作。但是,每次我在不同的浏览器中打开gmail账户时,我都会遇到一个问题。有没有办法只登录一次帐户,因为我需要用我正在登录的电子邮件在我的客户谷歌日历中设置事件。
using System;
using System.Web.Mvc;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Drive.v2;
using Google.Apis.Util.Store;

namespace Google.Apis.Sample.MVC4
{
    public class AppFlowMetadata : FlowMetadata
    {
        private static readonly IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = "PUT_CLIENT_ID_HERE",
                        ClientSecret = "PUT_CLIENT_SECRET_HERE"
                    },
                    Scopes = new[] { DriveService.Scope.Drive },
                    DataStore = new FileDataStore("Drive.Api.Auth.Store")
                });

        public override string GetUserId(Controller controller)
        {
            // In this sample we use the session to store the user identifiers.
            // That's not the best practice, because you should have a logic to identify
            // a user. You might want to use "OpenID Connect".
            // You can read more about the protocol in the following link:
            // https://developers.google.com/accounts/docs/OAuth2Login.
            var user = controller.Session["user"];
            if (user == null)
            {
                user = Guid.NewGuid();
                controller.Session["user"] = user;
            }
            return user.ToString();

        }

        public override IAuthorizationCodeFlow Flow
        {
            get { return flow; }
        }
    }
}