问题描述
我编写了一个应用程序,用于同步用户的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 (将#修改为@)