Jump to content


Photo

Emby API in PHP


  • Please log in to reply
35 replies to this topic

#1 SilvorMike1 OFFLINE  

SilvorMike1

    Newbie

  • Members
  • 3 posts

Posted 12 January 2020 - 06:19 PM

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!

#2 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 12 January 2020 - 06:47 PM

Hi, haven't seen any php but we have lots of javascript:

 

https://github.com/M...ient.Javascript



#3 SilvorMike1 OFFLINE  

SilvorMike1

    Newbie

  • Members
  • 3 posts

Posted 13 January 2020 - 03:25 AM

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.

#4 SilvorMike1 OFFLINE  

SilvorMike1

    Newbie

  • Members
  • 3 posts

Posted 13 January 2020 - 04:22 AM

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

#5 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 13 January 2020 - 03:05 PM

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.



#6 auroras1_mbinvites OFFLINE  

auroras1_mbinvites

    Newbie

  • Members
  • 1 posts
  • Local time: 08:12 AM

Posted 16 January 2020 - 07:24 PM

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



#7 symphonichorizon OFFLINE  

symphonichorizon

    Advanced Member

  • Members
  • 30 posts
  • Local time: 09:12 AM

Posted 16 January 2020 - 07:26 PM

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. 



#8 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 11:57 AM

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:...f6e3a8/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:...f6e3a8/Password
 
My other script, doing the same but creating a user - works perfectly. This doesn't work.


#9 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 26 January 2020 - 01:02 PM

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



#10 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 01:28 PM

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.



#11 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 01:33 PM

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."



#12 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 26 January 2020 - 01:34 PM

Yes just search the file for the url to find the code for it:

https://github.com/M...client.js#L2607



#13 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 01:41 PM

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.
 


#14 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 01:49 PM

To confirm - tried again

2020-01-26 18:48:47.662 Info HttpServer: HTTP POST http://emby.domain.c...06d253/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.c...06d253/Password



#15 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 04:05 PM

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?



#16 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 26 January 2020 - 04:07 PM

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.



#17 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 04:09 PM

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.



#18 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 26 January 2020 - 04:25 PM

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, 26 January 2020 - 04:33 PM.


#19 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 156872 posts
  • Local time: 09:12 AM

Posted 26 January 2020 - 08:43 PM

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



#20 SilvorMike OFFLINE  

SilvorMike

    Member

  • Members
  • 14 posts
  • Local time: 02:12 PM

Posted 27 January 2020 - 03:40 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users