Google MVC Auth问题:AppFlowMetadata'扔了一个例外。但是代码在本地起作用
首先:我已经看到了其他几个线程在谈论类似问题,但是这些线程的答案只是指向我在代码中用于参考的页面,因此请不要仅仅因为它看起来相似而杀死它。
我会收到错误“'Models.AppFlowMetadata'的类型初始化器抛出了一个例外。”
,但仅在生产IIS实例上。这在本地上正常工作,我已经检查了返回URLS (无论如何这给出了不同的错误)我唯一能想到的是,该验证验证是某种程度上被阻止了。 Google Dev Console没有显示错误。
该错误在以下代码中发生:
private static string[] scopes = { SheetsService.Scope.Spreadsheets, DriveService.Scope.Drive };
private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer {
ClientSecrets = new ClientSecrets {
ClientId = "[App id].apps.googleusercontent.com",
ClientSecret = "[secret]" },
Scopes = scopes,
DataStore = new FileDataStore("Drive.Api.Auth.Store") });
public override IAuthorizationCodeFlow Flow { get { return flow; } }
以上代码基于Google网站上给出的身份验证示例: for MVC的Google API auth
上述代码是从以下方式调用的:
private async Task<UserCredential> GetGoogleCreds()
{
Google.Apis.Auth.OAuth2.Web.AuthorizationCodeWebApp.AuthResult result;
try
{
var log = LogManager.GetCurrentClassLogger();
log.Info("GetGoogleCreds: getting temp appflowmetadata");
var temp = new Models.AppFlowMetaData();
result = await new AuthorizationCodeMvcApp(this, new Models.AppFlowMetaData()).AuthorizeAsync(new CancellationTokenSource().Token);
if (result.Credential == null)
{
LoggingIntoGoogle = null;
throw new ApiException(500, result.RedirectUri);
}
return result.Credential;
}
catch(ApiException ex)
{
throw ex;
}
catch(Exception ex)
{
ERROR(ref ex);
throw ex;
}
return null;
}
上面抛出的apiexception在上面抛出的apiexception告诉呼叫过程Google尚未登录,因此,它将其重定向到Google Oauth的结果(何时可以得到那么远)。
First: I have seen several other threads talking about similar issues, however those threads answers simply pointed to the pages I used for reference in my code, so please do not kill this off simply because it looks similar.
I am getting the error "The type initializer for 'Models.AppFlowMetaData' threw an exception."
BUT only on the production IIS instance. This works fine on local, I have triple checked the return urls (this gives a different error anyway) the only thing I can think of is that the Auth is being blocked somehow. Google dev console shows no errors.
The error is occurring in the below code:
private static string[] scopes = { SheetsService.Scope.Spreadsheets, DriveService.Scope.Drive };
private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer {
ClientSecrets = new ClientSecrets {
ClientId = "[App id].apps.googleusercontent.com",
ClientSecret = "[secret]" },
Scopes = scopes,
DataStore = new FileDataStore("Drive.Api.Auth.Store") });
public override IAuthorizationCodeFlow Flow { get { return flow; } }
The above code is based on the authentication example given on google's website here: Google API Auth for MVC
The above code is being called from:
private async Task<UserCredential> GetGoogleCreds()
{
Google.Apis.Auth.OAuth2.Web.AuthorizationCodeWebApp.AuthResult result;
try
{
var log = LogManager.GetCurrentClassLogger();
log.Info("GetGoogleCreds: getting temp appflowmetadata");
var temp = new Models.AppFlowMetaData();
result = await new AuthorizationCodeMvcApp(this, new Models.AppFlowMetaData()).AuthorizeAsync(new CancellationTokenSource().Token);
if (result.Credential == null)
{
LoggingIntoGoogle = null;
throw new ApiException(500, result.RedirectUri);
}
return result.Credential;
}
catch(ApiException ex)
{
throw ex;
}
catch(Exception ex)
{
ERROR(ref ex);
throw ex;
}
return null;
}
The APIException being thrown above tells the calling process Google has not been logged in yet, so it redirects to the Google OAuth result (when it can get that far).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这里的问题是Filedatastore,缺少Google实际记录了事物。
如果流动中的归档子公司与网络示例一样,则将在适当的IIS服务器上失败。
它试图创建一个文件夹(示例表明它使用了Google驱动器,但不使用Windows限制文件夹(ProgramData等),这当然会在甚至在一个上引起各种错误低安全服务器设置。
您必须使用
server.mappath()
并将其映射到本地网站文件夹,它将能够写入app_data。在上面的示例中,我通过使用其他组合来使用Google在App_Data下方使用一个文件夹来使其正常工作:
Ok the issue here is the FileDataStore and the lack of Google actually documenting things.
If the filedatastore in the flow is left as is in the web example it will fail on a proper IIS server.
It tries to create a folder (the example indicates it uses google drive, but it doesn't) under the windows restricted folders (programdata etc.), this of course causes all kinds of errors on even a low-security server setup.
You must use
Server.MapPath()
and map to a local website folder it will be able to write to such as app_data.For the example above I got it working by letting google use a folder underneath App_Data by using an additional combine: