Azure webjobs failed [Access is denied]

2019-09-15 05:12发布

问题:

I created a console application to read gmail inbox.I need my console app to read emails every 15 minutes.So i published the console app as webjobs with runmode : "On demand" and published the app using azure services (web apps).

But my webjobs doesn't seem to run and there is no option to schedule a webjob when i select publish as azure webjobs.

As soon as i publish i get this error in visual studio :

'ICICIEmailParser.sstageEntities-Web.config Connection String' argument cannot be null or empty

The webjob has no scheduled time so i manually ran the webjob from azure portal and when viewed log i see below error

[01/03/2017 06:48:38 > 7e6d88: SYS INFO] Status changed to Initializing [01/03/2017 06:48:38 > 7e6d88: SYS INFO] Job directory change detected: Job file 'ICICIEmailParser.application' timestamp differs between source and working directories. [01/03/2017 06:48:39 > 7e6d88: SYS INFO] Run script 'ICICIEmailParser.exe' with script host - 'WindowsScriptHost' [01/03/2017 06:48:39 > 7e6d88: SYS INFO] Status changed to Running [01/03/2017 06:48:40 > 7e6d88: ERR ] [01/03/2017 06:48:40 > 7e6d88: ERR ] Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Net.HttpListenerException: Access is denied [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Net.HttpListener.SetupV2Config() [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Net.HttpListener.Start() [01/03/2017 06:48:40 > 7e6d88: ERR ] at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.d__6.MoveNext() [01/03/2017 06:48:40 > 7e6d88: ERR ] --- End of stack trace from previous location where exception was thrown --- [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.d__8.MoveNext() [01/03/2017 06:48:40 > 7e6d88: ERR ] --- End of stack trace from previous location where exception was thrown --- [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__4.MoveNext() [01/03/2017 06:48:40 > 7e6d88: ERR ] --- End of stack trace from previous location where exception was thrown --- [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [01/03/2017 06:48:40 > 7e6d88: ERR ] at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.d__1.MoveNext() [01/03/2017 06:48:40 > 7e6d88: ERR ] --- End of inner exception stack trace --- [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Threading.Tasks.Task1.get_Result() [01/03/2017 06:48:40 > 7e6d88: ERR ] at GmailQuickstart.Program.d__3.MoveNext() [01/03/2017 06:48:40 > 7e6d88: ERR ] --- End of inner exception stack trace --- [01/03/2017 06:48:40 > 7e6d88: ERR ] at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken) [01/03/2017 06:48:40 > 7e6d88: ERR ] at GmailQuickstart.Program.Main(String[] args) [01/03/2017 06:48:40 > 7e6d88: SYS INFO] Status changed to Failed [01/03/2017 06:48:40 > 7e6d88: SYS ERR ] Job failed due to exit code -532462766

Things i have done :

I have a client-secret.json file of google oauth. I have added google secret and client id in my azure web app authentication/authorization I set javascript origin and redirect uri in google developer console

This is my main method :

static void Main(string[] args)
    {

        Task.WaitAll(GetEmail());


    }
static async Task GetEmail() {
//my c# code to read gmail inbox...

}

Any help would be appreciated !

Thanks in advance

回答1:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Net.HttpListenerException: Access is denied

According to your description, I assumed that you have followed .NET Quickstart to create your .NET console application that makes requests to the Gmail API. I followed the above tutorial and deployed it as Azure WebJobs, then I could reproduce this error. When running your command-line application (WebJob) on Azure, it would prompt you to authorize access and open a new window or tab in the default browser of the web server. However Azure WebJobs work as a background task under behind the website hosted on Azure for doing regular jobs and batch work, the end-users couldn't Interact with your WebJob to finish the authorization.

But my webjobs doesn't seem to run and there is no option to schedule a webjob when i select publish as azure webjobs.

As I known, you could leverage the following approaches to schedule your WebJob:

  • Use TimerTrigger from Azure WebJobs SDK Extensions

  • Use Azure Scheduler job to trigger your WebJob with some schedule

  • Schedule Azure WebJobs with cron expressions

I have added google secret and client id in my azure web app authentication/authorization I set javascript origin and redirect uri in google developer console

Per my understanding, you are trying to authenticate/authorize your web app with Google Account and schedule the WebJob to read messages from Gmail with the logged UserCredential. As I known, WebJobs couldn't directly communicate with your web app, I assumed that you need to store the logged UserCredential and schedule your webjob to retrieve the UserCredential to read messages from Gmail API.