Getting entities in web services content

Hey Guys,

I built a custom Web Services API with functions registered via elgg_ws_expose_function() for posting messages to the social network. I know that there are plugins like hypeGraph that can do this in combination with hypeWall, but because I need to do additional processing with the incoming data they do not fit for my use case.

Currently I am running into problems when trying to get an entity by its guid. The call is

get_entity($guid)

and this works fine in other plugins, but when I use it in my WS functions, it just returns false. As I did everything to ensure that an Integer is passed as parameter and the corresponding entity definitely exists, I am running out of ideas what the problem could be.

I thought, maybe one of you ran into similar problems or anybody has an idea what to do to make it work?

Are there any limitations when using such a function in web services content? But get_user($guid) works perfectly fine, so I think this is not the point.

I hope that somebody can help me, because this would be really important for the site functionality I need to offer.

Thanks already

  • API authentication allows you query the site as if you were a logged out user. You need to authenticate a user that has access to the entity. Users have ACCESS_PUBLIC, that's why you can see them.

  • Thank you, that was the tip I needed. Because I use a standard user for all remote posts, I did not have to use API authentication. Instead I used login($user) before accessing an entity. This might probably be not the best / cleanest solution, but it works fine for my purposes ;-)

  • Probably the easiest solution with the current implementation. I don't think there is currently a method to authenticate a user with a token (without a password).

    Don't forget to kill session (logout()) after you finish, just to be sure :)

     

  • You may not want to deal with the user events (and logging false logins) on every request:

    login($user) --> elgg_get_session()->setLoggedInUser($user)

    logout() --> elgg_get_session()->removeLoggedInUser()

  • Yes I already thought about occurring problems when logging in the user like this. And I think there was something like the current logged_in_user_guid being set to 0 when using logout() for some time even if another user is still logged in, but I can't remember more details on this. Then I saw that login($user) does not log in the user persistantly - which would need the second parameter - so I more or less hoped that this would also work without calling logout() afterwards.

    In the next days I will definitely try out what Steve suggested, which seems to be a cleaner solution.

    login($user) --> elgg_get_session()->setLoggedInUser($user)

    logout() --> elgg_get_session()->removeLoggedInUser()

    It sounds to me as if it would have less impact on event triggering and maybe also on things like elgg_get_logged_in_user_guid(). Could you maybe give me a little more information about the differences between the two calls?