botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

SetUserDataAsync throws a "MissingProperty" error

Open jakobpn opened this issue 10 years ago • 15 comments

I'm working on adding authentication to a basic bot using code from Jeff Hollan's VSTS Bot sample (https://github.com/jeffhollan/BotFrameworkSample).

My login controller is using SetUserDataAsync to save the access token for the user.

                var client = new ConnectorClient(Constants.botId, Constants.botSecret);
                BotData getData;
                try
                {
                    getData = await client.Bots.GetUserDataAsync(Constants.botId, userid);
                }
                catch
                {
                    getData = new BotData();
                }
                getData.Data = ar.Serialize();
                await client.Bots.SetUserDataAsync(Constants.botId, userid, getData); 

Unfortunately, the SetUserDataAsync method throws a "MissingProperty" error.

I'm running the bot locally and I am talking to it through the Bot emulator.

Here is the request that is sent to the Bot service:

POST https://api.botframework.com/bot/v1.0/bots/{removed}Bot/users/2c1c7fa3 HTTP/1.1 Ocp-Apim-Subscription-Key: {removed} Authorization: Basic {removed} User-Agent: Microsoft.Bot.Connector.ConnectorClient/1.0.0.0 Content-Type: application/json; charset=utf-8 Host: api.botframework.com Content-Length: 3823 Expect: 100-continue

{ "data": "{"AccessToken":"{removed}}" }

This is the response from the service:

HTTP/1.1 400 Bad Request Cache-Control: no-cache Pragma: no-cache Content-Length: 144 Content-Type: application/json; charset=utf-8 Expires: -1 X-Content-Type-Options: nosniff Set-Cookie: ARRAffinity={removed};Path=/;Domain=bc-api.trafficmanager.net X-Powered-By: ASP.NET Date: Sat, 16 Apr 2016 15:48:57 GMT

{ "error": { "code": "MissingProperty", "message": "You have to specificy userId or conversationId", "statusCode": 400 } }

And here is the exception:

{ "message": "An error has occurred.", "exceptionMessage": "You have to specificy userId or conversationId", "exceptionType": "Microsoft.Rest.HttpOperationException", "stackTrace": " at Microsoft.Bot.Connector.ErrorHandling.HandleError[ObjectT](HttpOperationResponse1 result)\r\n at Microsoft.Bot.Connector.BotsExtensions.<SetUserDataAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at Bot_Application1.LoginController.d__1.MoveNext() in C:\Users\jakobpn\Scratch\Bot Application1\Bot Application1\Controllers\LoginController.cs:line 49\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()" }

jakobpn avatar Apr 16 '16 16:04 jakobpn

@jakobpn Workaround: After login, you can send a message with the updated UserData:

var connector = new ConnectorClient();

Message message = new Message
{
  Text = "LOGIN_SUCCESS"
  // Cleared
};

BotData botUserData = await connector.Bots.GetUserDataAsync(botId, userId);
message.BotUserData = botUserData.Data;
message.SetBotUserData("AccessToken", accessToken);
await connector.Messages.SendMessageAsync(message);

wiltodelta avatar Apr 18 '16 04:04 wiltodelta

Thanks @wiltodelta. I was wondering how I could send a message in the chat once the login succeded and I will give your code a try to see how it works. However, in my case the call to GetUserDataAsync throws an "object not found" error, so I will also need a workaround for that.

jakobpn avatar Apr 18 '16 15:04 jakobpn

@wiltodelta Tried the code and got a "Object reference not set to an instance of an object" exception from SendMessageAsync. I don't think the message object has enough info in it to send a message back to the conversation with the user.

jakobpn avatar Apr 18 '16 15:04 jakobpn

Does your userid passed to GetUserDataAsync/SetUserDataAsync have a non-null value?

willportnoy avatar Apr 18 '16 16:04 willportnoy

@willportnoy yes, it is "2c1c7fa3", a value I grabbed from the message.From.Id property.

jakobpn avatar Apr 18 '16 16:04 jakobpn

I believe we resolved this offline as a mismatch between production and the emulator - feel free to reopen if not.

willportnoy avatar Apr 19 '16 16:04 willportnoy

@jakobpn @willportnoy I'm having the same issue. Could you please share what was the resolution?

Thanks in advance!

ejadib avatar Apr 20 '16 20:04 ejadib

I'm also having the same issue. The getting and setting of data using connector.Bots doesn't seem to work. It's not a difference between the emulator and production as far as I can tell since if I deploy my bot to Telegram, I can see it's also not working.

My particular issue is using connector.Bots.SetConversationDataAsync(ReceivedMessage.To.Id, ReceivedMessage.ConversationId, botData)

Calling this I get: Microsoft.Rest.HttpOperationException: You have to specificy userId or conversationId

In the debugger I can see that both ReceivedMessage.To.Id and ReceivedMessage.ConversationId are not null, they both have reasonable values. The ReceivedMessage object is the one the user has sent me, so is populated correctly.

Creating a reply message and setting the data on that does work fine, however it's not a good workaround since the reason I wanted to use this API is because I can have concurrency problems and for Concurrency it's recommended to use ConnectorClient in the docs.

JohnDuncanScott avatar Apr 29 '16 16:04 JohnDuncanScott

@duscott Try using RecievedMessage.From.Id instead of RecievedMessage.To.Id.

jakobpn avatar Apr 29 '16 17:04 jakobpn

No that’s not right, you need to use the userid of the user you are wanting to store it with

This is a bug on our side, I am about to check in a fix for it

From: Jakob Nielsen-MSFT [mailto:[email protected]] Sent: Friday, April 29, 2016 10:21 AM To: Microsoft/BotBuilder [email protected] Cc: Tom Laird-McConnell [email protected]; Assign [email protected] Subject: Re: [Microsoft/BotBuilder] SetUserDataAsync throws a "MissingProperty" error (#124)

@duscotthttps://github.com/duscott Try using RecievedMessage.From.Id instead of RecievedMessage.To.Id.

— You are receiving this because you were assigned. Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/BotBuilder/issues/124#issuecomment-215821145

tomlm avatar Apr 29 '16 17:04 tomlm

OK, I have the resolution for this and it is not a bug on our side but a documentation bug. (I will update the code to give better error message)

You will get this behavior if you are trying to use a BotData with a null etag.

The correct pattern should always be: // get the BotDAta Var botData = Client.Bots.GetXXXData(…); // change the botDAta botData.Data = {…}; // Submite the new botData client.Bots.SetXXXData(botData);

If you do this you always get concurrent behavior, because the botData that is returned will have the ETag and round-trip it.

What you can’t do is Clients.Bots.SetXXXData(new BotData() { Data = {….}} ); Because the ETag will be null.

From: duscott [mailto:[email protected]] Sent: Friday, April 29, 2016 9:01 AM To: Microsoft/BotBuilder [email protected] Cc: Tom Laird-McConnell [email protected]; Assign [email protected] Subject: Re: [Microsoft/BotBuilder] SetUserDataAsync throws a "MissingProperty" error (#124)

I'm also having the same issue. The getting and setting of data using connector.Bots doesn't seem to work. It's not a difference between the emulator and production as far as I can tell since if I deploy my bot to Telegram, I can see it's also not working.

My particular issue is using connector.Bots.SetConversationDataAsync(ReceivedMessage.To.Id, ReceivedMessage.ConversationId, botData)

Calling this I get: Microsoft.Rest.HttpOperationException: You have to specificy userId or conversationId

In the debugger I can see that both ReceivedMessage.To.Id and ReceivedMessage.ConversationId are not null, they both have reasonable values. The ReceivedMessage object is the one the user has sent me, so is populated correctly.

Creating a reply message and setting the data on that does work fine, however it's not a good workaround since the reason I wanted to use this API is because I can have concurrency problems and for Concurrency it's recommended to use ConnectorClient in the docs.

— You are receiving this because you were assigned. Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/BotBuilder/issues/124#issuecomment-215778352

tomlm avatar Apr 29 '16 17:04 tomlm

Hello,

I tried your sample, it still doesnt work for me. What am I missing? I still get this error:

You have to specificy valid userId or conversationId

on this line

        BotData botData = await connectorClient.Bots.GetConversationDataAsync(botAppId, model.ConversationId);

How come a new conversation with a conversationId is not valid.

string botAppId = CloudConfigurationManager.GetSetting("BotAppId");

        DirectLineClient client = new DirectLineClient(CloudConfigurationManager.GetSetting("BotDirectLineSecret"));
        Conversation conversation = await client.Conversations.NewConversationAsync();

        BotClientModel model = new BotClientModel();
        model.ConversationId = conversation.ConversationId;
        model.ETag = conversation.ETag;
        model.Token = conversation.Token;

        ConnectorClient connectorClient = new ConnectorClient(botAppId, CloudConfigurationManager.GetSetting("BotSecret"));

        BotData botData = await connectorClient.Bots.GetConversationDataAsync(botAppId, model.ConversationId);
        botData.SetProperty("MemberId", this.CurrentMember.Id);

        await connectorClient.Bots.SetConversationDataAsync(botAppId, model.ConversationId, botData);

ahmet8282 avatar May 05 '16 21:05 ahmet8282

There's a mismatch between the emulator and what happens in Azure. If you deploy to Azure, you don't get that message, it only happens when testing locally via the emulator. This still happens for me for version 1.0.0.64 of the emulator.

My hack at the moment is to store the data on the message when I'm in Debug and working locally, and then switching to using the REST services in Release. This isn't a nice solution and still makes it hard to test, but I believe the team are aware of the issue and I imagine the next update will fix it.

JohnDuncanScott avatar May 06 '16 13:05 JohnDuncanScott

I'm not using the emulator though. This code is executed by a regular http request. I assume this code should work even on a console app. am I wrong?

ahmet8282 avatar May 06 '16 16:05 ahmet8282

moved to #241 since this issue is closed.

ahmet8282 avatar May 07 '16 22:05 ahmet8282