如何解决Google Calendar API异常“访问被拒绝” .NET Core Web App在本地运行良好,在Azure上发布时引发异常
我编写了一个应用程序,用于同步用户的Google日历活动。当我在本地运行该应用程序时,该应用程序运行良好,但是当以天蓝色发布时,它会引发以下异常。
System.Net.HttpListenerException(5):访问被拒绝。
应用程序详细信息: ASP.NET MVC核心,C#
我已经在Google Application Console上为Web Application创建了客户端ID。
堆栈跟踪的某些部分如下:
System.Net.HttpListenerException (5): Access is denied.
at System.Net.HttpListener.SetupV2Config()
at System.Net.HttpListener.Start()
at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener()
at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.ReceiveCodeAsync(AuthorizationCodeRequestUrl url,CancellationToken taskCancellationToken)
at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.AuthorizeAsync(String userId,CancellationToken taskCancellationToken)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationbroker.AuthorizeAsync(Initializer initializer,IEnumerable`1 scopes,String user,CancellationToken taskCancellationToken,IDataStore dataStore,ICodeReceiver codeReceiver)
at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationbroker.AuthorizeAsync(ClientSecrets clientSecrets,ICodeReceiver codeReceiver)
我正在使用以下代码(这会导致异常):
private CalendarService GetCalendarService(string username)
{
UserCredential credential;
//string fileName = "installed_app_secret.json";
string fileName = "web_app_secret.json";
string[] scopes = new string[] { CalendarService.Scope.Calendar,CalendarService.Scope.CalendarReadonly };
string clientSecretFile = Path.Combine(_webrootPath,"App_Data",fileName);
using (var stream = new FileStream(clientSecretFile,FileMode.Open,FileAccess.Read))
{
credential = GoogleWebAuthorizationbroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,scopes,username,CancellationToken.None,new DbDataStore(this._configuration,_attorneyId)).Result;
}
// Create Google Calendar API service.
return new CalendarService(new BaseClientService.Initializer()
{
HttpClientinitializer = credential,ApplicationName = _APPLICATION_NAME,});
}
此外,我还为Web应用程序(未安装的应用程序)生成了ClientID。 Json文件如下所示:
{
"web": {
"client_id": "xxxxxx","project_id": "calendar-sync-app-290311","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs","client_secret": "yyyyy","redirect_uris": [ "urn:ietf:wg:oauth:2.0:oob","https://<path>/oauth2callback" ]
}
}
在此方面的任何帮助都将受到高度赞赏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。