Authorization for Microsoft App ID Error Sending Facebook Messages via WebJob
Hi,
My bot currently works fine when interacting via facebook/Skype. However when i try to send a message to a Facebook user which I have id/names of through a WebJob, I get this error :
Unhandled Exception: System.UnauthorizedAccessException: Authorization for Microsoft App ID failed with status code Forbidden [10/21/2016 10:05:30 > 0acdf9: ERR ] at Microsoft.Bot.Connector.JwtTokenRefresher.
d__2.MoveNext() [10/21/2016 10:05:30 > 0acdf9: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at Microsoft.Bot.Connector.Conversations. d__5.MoveNext() [10/21/2016 10:05:30 > 0acdf9: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at Microsoft.Bot.Connector.ConversationsExtensions. d__1.MoveNext() [10/21/2016 10:05:30 > 0acdf9: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at WebJob1.Program. d__2.MoveNext() [10/21/2016 10:05:30 > 0acdf9: ERR ] --- End of stack trace from previous location where exception was thrown --- [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [10/21/2016 10:05:30 > 0acdf9: ERR ] at WebJob1.Program. d__1.MoveNext()
I am new to both Webjobs and MicrosoftBot. I am unsure if I should be doing this at all or if what im trying to do is supported. What I was trying to do was make the bot send a facebook message to a user based on a schedule/trigger.
This is the code im running on the WebJob:
var connector = new ConnectorClient(new Uri(facebookURL));
var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
Trace.TraceInformation($"conversationId.Id : {conversationId.Id}");
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.Text = "On My Own Hello Direct Conversation Async";
await connector.Conversations.SendToConversationAsync((Activity)message);
Trace.TraceInformation($"SendMessage Async Done");
Since I was running the code via WebJobs, the credentials was not in the settings, all i had to do was pass the MicrosoftAppId and MicrosoftAppPassword in the new ConnectorClient constructor.