如何解决redirect_uri_mismatch i ASP.NET MVC?
How to solve redirect_uri_mismatch i ASP.NET MVC?
我已经创建了 ASP.NET MVC 网络应用程序以与 google 日历集成 API。
我已遵循 Google 站点中的文档。我在 Google 开发者控制台中设置项目如下:
我在创建项目后也下载了凭据文件,但在使用它时我每次都收到 uri_mismatch
错误。
如何解决?
我的授权码是:
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 是在库中配置的
uri_mismatch
的解决方案
在Google开发者控制台添加
http://localhost:63972/authorize
或
http://127.0.0.1:63972/authorize
但请确保您在 IDE (visual studio) 中设置了静态端口,否则您将再次遇到同样的问题。
提示 Web 应用程序与安装的应用程序
您发布的代码使用了 GoogleWebAuthorizationBroker.AuthorizeAsync
,它专为与已安装的应用程序一起使用而设计。当授权 window 打开时,它将在代码为 运行 的机器上打开。当您在开发中时,它将在您的开发机器上打开,但当您尝试将其发布到服务器时,它不会工作。
您应该使用 GoogleAuthorizationCodeFlow
来遵循此示例 Web applications (ASP.NET MVC)
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; }
}
}
}
我已经创建了 ASP.NET MVC 网络应用程序以与 google 日历集成 API。
我已遵循 Google 站点中的文档。我在 Google 开发者控制台中设置项目如下:
我在创建项目后也下载了凭据文件,但在使用它时我每次都收到 uri_mismatch
错误。
如何解决?
我的授权码是:
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 是在库中配置的
uri_mismatch
的解决方案在Google开发者控制台添加
http://localhost:63972/authorize
或
http://127.0.0.1:63972/authorize
但请确保您在 IDE (visual studio) 中设置了静态端口,否则您将再次遇到同样的问题。
提示 Web 应用程序与安装的应用程序
您发布的代码使用了 GoogleWebAuthorizationBroker.AuthorizeAsync
,它专为与已安装的应用程序一起使用而设计。当授权 window 打开时,它将在代码为 运行 的机器上打开。当您在开发中时,它将在您的开发机器上打开,但当您尝试将其发布到服务器时,它不会工作。
您应该使用 GoogleAuthorizationCodeFlow
来遵循此示例 Web applications (ASP.NET MVC)
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; }
}
}
}