Media.Get not returning media post

I have a section of code that is used in 2 scenarios.  This code is a call to Telligent In-Process API to get a media post.

post = teeAPIv1.PublicApi.Media.Get(post_ID);

teeAPIv1 is set as follows:

using teeAPIv1 = Telligent.Evolution.Extensibility.Api.Version1;

post_ID is an integer, and in both scenarios the value of 63 is passed in.

On the first scenario, it returns a successfully filled out media post object.

On the 2nd scenario, it returns a media post object whose properties are all null.  Basically worthless. (Image of the failed returned object attached)

I'm assuming some sort of context is at play here but I'm not seeing it. 

What can make this call fail in 1 of 2 scenarios when handing it the same value to look up?

Parents
  • There could be a number of issues at play in the two scenarios. The likely reason for the "null" object is that an error is occurring on the second call. The error message should be accessible via the base class ApiEntity.Errors collection.

  • I should clarify.  The actual error in the error collection is that I don't have 'read group' permission for the group.  

    Since this process happens outside of the normal request/response cycle, there's no normal page context present to pass around the proper permissions.  My call needs to succeed without that context.

  • If this code is running outside the normal Telligent context you'll have to provide this context.

    You should be able to use the RunAsUser method on the Users service to do this, but will need to have a user that has appropriate access to the group to run the code as. You should be able to check if there is an appropriate user context available and then fall back to a specified user if needed.

    Haven't tried to run/compile this, but something along these lines should help set the context correctly for your Get call.

    var users = Apis.Get<IUsers>();
    
    var runAsUserId = users.AccessingUser.Id;
    
    // May be the Anonymous user if no context is available so may want to different 
    // or additional checks here based on your needs
    if (runAsUserId == null)
    {
        runAsUserId = USER_ID_OF_THE_FALLBACK_USER_TO_RUN_AS;
    }
    
    users.RunAsUser(runAsUserId, () => 
    {
        post = teeAPIv1.PublicApi.Media.Get(post_ID);
    });

    Run as user allows the code to run with a fixed account, be careful to make sure your not elevating privilages for users in the other scenarios unintentionally.

  • Thank you Rhys, I'll give it a shot with using the group owner's ID, which should satisfy the security conditions. 

  • That worked well Rhys, unfortunately this code is also used by another section, and implementing the needed credentials with this method would open a security hole for the other one.

    We're going to bite the bullet and create a custom REST end point for this. 

    I'm marking your reply as answer though, as it was a working solution.  Thanks!

Reply Children
No Data