Jump to content
SilvorMike1

Emby API in PHP

Recommended Posts

SilvorMike1

Has anyone come across any PHP scripts to

Invite a user to an emby share

Delete a user from an emby share

 

That's literally all I need. Invite and delete which I intend to build in to a function.

 

Thanks!

Share this post


Link to post
Share on other sites
SilvorMike1

Thank you. I'll use Chrome to establish URLs and presumably JSON data sent in the http body to create and delete a user using the post and delete methods and see if I can knock a script together. Was just trying to save time. Haha.

Share this post


Link to post
Share on other sites
SilvorMike1

Is there a URL with an API key and the user email that would give me their UID?

Share this post


Link to post
Share on other sites
Luke

Is there a URL with an API key and the user email that would give me their UID?

 

Not exactly, but if you check out the built-in api documentation in the server dashboard, there is a /Users endpoint that may help you.

Share this post


Link to post
Share on other sites
auroras1_mbinvites

I have a whmcs module that uses php to do this.

Share this post


Link to post
Share on other sites
symphonichorizon

sorry logged into wrong account, but we have a whmcs module that makes local accounts invites suers via connect removes users and allows password changes among other functions. 

Share this post


Link to post
Share on other sites
SilvorMike
I've got the API to add a user now, but when trying to change their password it seems to fail.

 

Posting this data: {"CurrentPw":"","NewPw":"testpassword"}

 


 

Server log:

 

2020-01-26 16:49:26.852 Info HttpServer: HTTP POST http://emby.url.com:8096/emby/Users/7297a4946bbd483287f7fdf8c2f6e3a8/Password. UserAgent:

2020-01-26 16:49:26.853 Error HttpServer: Error processing request

    *** Error Report ***

    Version: 4.3.1.0

    Command line: /opt/emby-server/system/EmbyServer.dll -programdata /var/lib/emby -ffdetect /opt/emby-server/bin/ffdetect -ffmpeg /opt/emby-server/bin/ffmpeg -ffprobe /opt/emby-server/bin/ffprobe -restartexitcode 3 -updatepackage emby-server-deb_{version}_amd64.deb

    Operating system: Unix 4.4.0.0

    64-Bit OS: True

    64-Bit Process: True

    User Interactive: True

    Runtime: file:///opt/emby-server/system/System.Private.CoreLib.dll

    Processor count: 8

    Program data path: /var/lib/emby

    Application directory: /opt/emby-server/system

    System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object.

     at MediaBrowser.Api.BaseApiService.AssertCanUpdateUser(IAuthorizationContext authContext, IUserManager userManager, Int64 userId, Boolean restrictUserPreferences)

     at MediaBrowser.Api.UserService.PostAsync(UpdateUserPassword request)

     at Emby.Server.Implementations.Services.ServiceController.GetTaskResult(Task task)

     at Emby.Server.Implementations.Services.ServiceHandler.ProcessRequestAsync(HttpListenerHost appHost, IRequest httpReq, IResponse httpRes, RestPath restPath, String responseContentType, CancellationToken cancellationToken)

     at Emby.Server.Implementations.HttpServer.HttpListenerHost.RequestHandler(IRequest httpReq, ReadOnlyMemory`1 urlString, ReadOnlyMemory`1 localPath, CancellationToken cancellationToken)

    Source: MediaBrowser.Api

    TargetSite: Void AssertCanUpdateUser(MediaBrowser.Controller.Net.IAuthorizationContext, MediaBrowser.Controller.Library.IUserManager, Int64, Boolean)

    

2020-01-26 16:49:26.853 Info HttpServer: HTTP Response 500 to 162.250.191.86. Time: 2ms. http://emby.url.com:8096/emby/Users/7297a4946bbd483287f7fdf8c2f6e3a8/Password

 

My other script, doing the same but creating a user - works perfectly. This doesn't work.

Share this post


Link to post
Share on other sites
Luke

Did you compare what you're doing to the javascript api libs i linked to before?

Share this post


Link to post
Share on other sites
SilvorMike

No - I'm not a java dev. I am a PHP dev. I've got create user working - can you point me in the right area for the java script so I can try and work out an alternative solution please?

 

Thank you.

Share this post


Link to post
Share on other sites
SilvorMike

I am literally creating local users - not using Emby Connect. It seems odd that you cannot create the user without a password at time of creation. The users add perfectly well - but not sure why the password is not being set properly. I tried it through PostMan, it works but not with my own API key - I get the error "Object reference not set to an instance of an object."

Share this post


Link to post
Share on other sites
SilvorMike

Ok great, so I've found the javascript :

   updateUserPassword(userId, currentPassword, newPassword) {
 
        if (!userId) {
            return Promise.reject();
        }
 
        const url = this.getUrl(`Users/${userId}/Password`);
        const serverId = this.serverId();
        const instance = this;
 
        return this.ajax({
            type: "POST",
            url: url,
            data: JSON.stringify({
                CurrentPw: currentPassword || '',
                NewPw: newPassword
            }),
            contentType: "application/json"
        }).then(() => {
            removeCachedUser(instance.appStorage, userId, serverId);
            return Promise.resolve();
        });
    }
 
Which is exactly what I am doing in the PHP equivalent.
 
$http_method1 = 'POST';
$http_body1 = json_decode('{"CurrentPw":"","NewPw":"testpassword"}');
$http_return1 = '200';
 
call_endpoint($http_link1, $http_method1, $http_body1);
 
However, seem to be getting error:
Object reference not set to an instance of an object.
 
What am I doing wrong? What does the above mean....
 
I've tried in PostMan - same response.
 
API token is correct, as I use it to create the user in the first place.
 

Share this post


Link to post
Share on other sites
SilvorMike

To confirm - tried again

2020-01-26 18:48:47.662 Info HttpServer: HTTP POST http://emby.domain.com:8096/emby/Users/e839d07dcff04c9884c16b097e06d253/Password. UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36
2020-01-26 18:48:47.664 Error HttpServer: Error processing request
    *** Error Report ***
    Version: 4.3.1.0
    Command line: /opt/emby-server/system/EmbyServer.dll -programdata /var/lib/emby -ffdetect /opt/emby-server/bin/ffdetect -ffmpeg /opt/emby-server/bin/ffmpeg -ffprobe /opt/emby-server/bin/ffprobe -restartexitcode 3 -updatepackage emby-server-deb_{version}_amd64.deb
    Operating system: Unix 4.4.0.0
    64-Bit OS: True
    64-Bit Process: True
    User Interactive: True
    Runtime: file:///opt/emby-server/system/System.Private.CoreLib.dll
    Processor count: 8
    Program data path: /var/lib/emby
    Application directory: /opt/emby-server/system
    System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object.
     at MediaBrowser.Api.BaseApiService.AssertCanUpdateUser(IAuthorizationContext authContext, IUserManager userManager, Int64 userId, Boolean restrictUserPreferences)
     at MediaBrowser.Api.UserService.PostAsync(UpdateUserPassword request)
     at Emby.Server.Implementations.Services.ServiceController.GetTaskResult(Task task)
     at Emby.Server.Implementations.Services.ServiceHandler.ProcessRequestAsync(HttpListenerHost appHost, IRequest httpReq, IResponse httpRes, RestPath restPath, String responseContentType, CancellationToken cancellationToken)
     at Emby.Server.Implementations.HttpServer.HttpListenerHost.RequestHandler(IRequest httpReq, ReadOnlyMemory`1 urlString, ReadOnlyMemory`1 localPath, CancellationToken cancellationToken)
    Source: MediaBrowser.Api
    TargetSite: Void AssertCanUpdateUser(MediaBrowser.Controller.Net.IAuthorizationContext, MediaBrowser.Controller.Library.IUserManager, Int64, Boolean)
    
2020-01-26 18:48:47.664 Info HttpServer: HTTP Response 500 to 194.35.233.16. Time: 3ms. http://emby.domain.com:8096/emby/Users/e839d07dcff04c9884c16b097e06d253/Password

Share this post


Link to post
Share on other sites
SilvorMike

So a little more investigation - I can seem to do it from Postman if I copy and paste the headers X-Emby-Authorization as seen in the Google Inspect mode. 

 

However, I can't alter these settings what-so-ever otherwise it doesn't work.

 

What headers should I be sending for my API to work on all API posts, using my generated API key?

Share this post


Link to post
Share on other sites
Luke

What headers should I be sending for my API to work on all API posts, using my generated API key?

If you use the api_key param you don't need the authorization header, however it's possible there might be a few api's here and there that won't work without it. It's something we'll have to look into.

 

For now, using the headers instead of api_key will work.

Share this post


Link to post
Share on other sites
SilvorMike

If you use the api_key param you don't need the authorization header, however it's possible there might be a few api's here and there that won't work without it. It's something we'll have to look into.

 

For now, using the headers instead of api_key will work.

Thanks Luke and I must say at this point - your dedication and work on this is outstanding. Your posts across the forum have helped me greatly and I for one sincerely appreciate your time and effort.

 

If I sent the headers, can I use my own API key - as trying that failed. But using the api key given in the google chrome developer inspect > network > password POST worked. Which is odd?

 

Can you confirm the headers I should be sending to avoid the error: Object reference not set to an instance of an object.

Share this post


Link to post
Share on other sites
SilvorMike

If I use these headers - it works

X-Emby-Authorization: MediaBrowser Client="Emby Web", Device="Chrome", DeviceId="TW96aWxsYS81LjAgKE1hY2ludG9zaDsgSW50ZWwgTWFjIE9TIFggMTBfMTJfNCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzc5LjAuMzk0NS4xMTcgU2FmYXJpLzUzNy4zNnwxNTc5NDQ5NjgyNTI2", Version="4.3.1.0", Token="TOKENGENERATEDINCHROME"

 

If I use these headers - it doesn't work with error: Object reference not set to an instance of an object.

X-Emby-Authorization: MediaBrowser Client="SilvorAPI", Device="Chrome", DeviceId="d2aed5d1-bd8e-4a90-a146-8bd529e1bf7a", Version="1.1.0.1", Token="MYOWNAPITOKEN"

 

But I don't want to re-use the first headers that work, over and over - as I presume the key will expire and it technically isn't the correct headers.

 

I'm presuming this is a bug somewhere :-( 

 

and to confirm, it seems to never work /Users/{userid}/Password?api_key=Key

 

Which isn't ideal. Can this be logged as a bug somewhere?

Edited by SilvorMike

Share this post


Link to post
Share on other sites
Luke

I would just use the Chrome debugger to look at what the web app is doing and copy that.

Share this post


Link to post
Share on other sites
SilvorMike

Can do that but will the token that chrome displays in the debugger expire? Because it isn't a token I've generated.

Share this post


Link to post
Share on other sites
Luke

Eventually yes.

Share this post


Link to post
Share on other sites
symphonichorizon

this one works well 

"X-Emby-Authorization: MediaBrowser Client=Android, Device=Samsung Galaxy SIII, DeviceId=xxx, Version=1.0.0.0";

and what we do is pull the auth key each time as you never know when it will expire so better safe then sorry.

Share this post


Link to post
Share on other sites
SilvorMike

How do you pull the auth key?

Share this post


Link to post
Share on other sites
chef

this one works well

"X-Emby-Authorization: MediaBrowser Client=Android, Device=Samsung Galaxy SIII, DeviceId=xxx, Version=1.0.0.0";
and what we do is pull the auth key each time as you never know when it will expire so better safe then sorry.

are you saying that you are authenticating on each request to get an authorization token?

Share this post


Link to post
Share on other sites
SilvorMike

are you saying that you are authenticating on each request to get an authorization token?

 

 

this one works well 

"X-Emby-Authorization: MediaBrowser Client=Android, Device=Samsung Galaxy SIII, DeviceId=xxx, Version=1.0.0.0";

and what we do is pull the auth key each time as you never know when it will expire so better safe then sorry.

 

Be great to know because - if that is possible it's a suitable way around it - although not ideal....can the ability to do it from using api_key in the URL not be fixed? 

 

Thank you,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...