我想在asp.net web api项目中使用gmail api。这就是我到目前为止所做的:
UserCredential credential;
var cleantSecretPath = HostingEnvironment.MapPath("~/client_secret.json");
var rootPath = HostingEnvironment.MapPath("~/");
if (rootPath != null)
{
var credentialPath = Path.Combine(rootPath, "Credentials");
var directoryInfo = Directory.CreateDirectory(credentialPath);
}
var credentialsPath = HostingEnvironment.MapPath("~/Credentials/");
using (var stream = new FileStream(cleantSecretPath, FileMode.Open, FileAccess.Read))
{
var secret = GoogleClientSecrets.Load(stream).Secrets;
var dataStore = new FileDataStore("test");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secret, Scopes, _credentials.Email, CancellationToken.None, dataStore).Result;
}
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
return
它适用于本地主机,但只要我将其部署到azure,我就会收到错误:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>One or more errors occurred.</ExceptionMessage>
<ExceptionType>System.AggregateException</ExceptionType>
<StackTrace>
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task`1.get_Result() at BL.GmailServiceBl.GetGmailService() in D:\Development\DotNetProjects\GmailAnalysis\BL\GmailServiceBl.cs:line 53 at BL.AnalysisService..ctor(PersonCredentials credentials) in D:\Development\DotNetProjects\GmailAnalysis\BL\AnalysisService.cs:line 22 at WebAPI.Controllers.ValuesController.Get(PersonCredentials credentials) in D:\Development\DotNetProjects\GmailAnalysis\WebAPI\Controllers\ValuesController.cs:line 63 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>Access is denied</ExceptionMessage>
<ExceptionType>System.Net.HttpListenerException</ExceptionType>
<StackTrace>
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext() in c:\code\github\google-api-dotnet-client\Tools\Google.Apis.Release\bin\Release\1.9.2\default\Src\GoogleApis.Auth.DotNet4\OAuth2\GoogleWebAuthorizationBroker.cs:line 59
</StackTrace>
</InnerException>
</Error>
我正在使用client_secret.json进行Web应用程序。有人可以给我一个例子,或指出我正确的方向。
答案 0 :(得分:0)
这是一个有效的例子:
var filePath = HostingEnvironment.MapPath("~/build-info.json");
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
string fileContent;
using (var reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
我已在本地运行并部署到Azure WebApp,它可以在两个位置运行。
所以类似下面的内容应该有效:
UserCredential credential;
var filePath = HostingEnvironment.MapPath("~/client_secret.json");
using (var stream = new FileStream(filePath , FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
new[] { GmailService.Scope.GmailReadonly },
"user", CancellationToken.None);
}
答案 1 :(得分:0)
在解决类似问题几天后,我发现这不是你应该怎么做的:
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secret, Scopes, _credentials.Email, CancellationToken.None, dataStore).Result;
相反,您应该使用FlowMetadata方法进行身份验证,demonstrated here.