Jump to content


Photo

Emby API in PHP


  • Please log in to reply
35 replies to this topic

#21 Luke OFFLINE  

Luke

    System Architect

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

Posted 27 January 2020 - 11:50 AM

Eventually yes.

#22 symphonichorizon OFFLINE  

symphonichorizon

    Advanced Member

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

Posted 30 January 2020 - 06:10 PM

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.



#23 SilvorMike OFFLINE  

SilvorMike

    Member

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

Posted 30 January 2020 - 06:12 PM

How do you pull the auth key?



#24 chef OFFLINE  

chef

    Advanced Member

  • Developers
  • 4730 posts
  • Local time: 09:12 AM
  • LocationPeterborough, Canada

Posted 30 January 2020 - 07:42 PM

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?

#25 SilvorMike OFFLINE  

SilvorMike

    Member

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

Posted 31 January 2020 - 06:48 AM

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,



#26 PenkethBoy OFFLINE  

PenkethBoy

    Advanced Member

  • Members
  • 4297 posts
  • Local time: 02:12 PM
  • LocationWarrington,UK

Posted 31 January 2020 - 06:54 AM

you only need to get the auth key once - as you login with a user

 

then use it on each subsequent call


  • chef likes this

#27 SilvorMike OFFLINE  

SilvorMike

    Member

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

Posted 31 January 2020 - 09:49 AM

you only need to get the auth key once - as you login with a user

 

then use it on each subsequent call

 

Thanks mate - where do you obtain your auth key once logged in - I got one from the chrome debugger in the authenticatebyuser POST command...

But not sure if the key will last or whether it will expire?

 

And it was within the headers. 



#28 chef OFFLINE  

chef

    Advanced Member

  • Developers
  • 4730 posts
  • Local time: 09:12 AM
  • LocationPeterborough, Canada

Posted 31 January 2020 - 01:06 PM

The auth token is created when you post credentials to "/Users/AuthenticateByName" with the proper headers mentioned above.

 

But as PenkethBoy mentioned, I wouldn't authenticate on each request... 

 

the other way to use the api, is to send the url request with "&api_key=" then the api key you create in the dashboard.

 

To best test and understand the api, I would work with the Swagger docs first.

 

click the "api" link at the bottom of the dashboard page.

 

The wiki has the work flow, but here is a quick refresher:

 

1. UDP broadcast on port 7359  "who is EmbyServer?"  to everything on the network.

2. response is ip and port data

3. request "users/public" to get a list of  users

4. get user ID you want

5. Using the  "X-Emby-Authorization" headers, POST headers, userName, password (encrypted in MD5 and Sha1) to "/Users/AuthenticateByName"

6. get access token

7. use access token in subsequent requests to the server under the header "X-Emby-Token".

 

There are ways to refresh the token, but off hand I can't remember, you'd have to reference the github wiki.


Edited by chef, 31 January 2020 - 01:21 PM.


#29 SilvorMike OFFLINE  

SilvorMike

    Member

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

Posted 31 January 2020 - 01:54 PM

Thanks Chef.

Got that working. I'll look at how to refresh token

The password doesn't seem to change using just ?api_key=key

#30 maxt.v OFFLINE  

maxt.v

    Newbie

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

Posted 07 April 2020 - 03:06 AM

Hi guys, 

I have this code in PHP

 

$name="NewUser";

$arrapre = array('Name'=>$name);

$datapost = json_encode($arrapre);
echo $datapost;
 
what I get is this
 
{"Name":"NewUser"}
 
when I send the POST to server.com:8096/emby/Users/New
 
adding this headders:
 
      'User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
      'Content-Type : application/x-www-form-urlencoded',
      'X-Emby-Token : embyAPIfromDashboard',
      'X-Emby-Client : Emby Web',
 
I got this error on server log
 
2020-04-07 05:58:36.456 Info HttpServer: HTTP POST http://server.com:8096/emby/Users/New. UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
2020-04-07 05:58:36.459 Error HttpServer: Error processing request
    *** Error Report ***
    Version: 4.4.0.40
    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.15.0.91
    64-Bit OS: True
    64-Bit Process: True
    User Interactive: True
    Runtime: file:///opt/emby-server/system/System.Private.CoreLib.dll
    System.Environment.Version: 3.1.2
    Processor count: 8
    Program data path: /var/lib/emby
    Application directory: /opt/emby-server/system
    System.ArgumentNullException: System.ArgumentNullException: Value cannot be null. (Parameter 'name')
     at Emby.Server.Implementations.Library.UserManager.CreateUser(String name, UserPolicy userPolicy)
     at MediaBrowser.Api.UserService.Post(CreateUserByName 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: Emby.Server.Implementations
    TargetSite: Void MoveNext()
    
2020-04-07 05:58:36.459 Info HttpServer: HTTP Response 400 to x.x.x.x. Time: 3ms. http://server.com:8096/emby/Users/New
 
 
what I'm missing or what I need to send to the server to add the user ?


#31 SilvorMike OFFLINE  

SilvorMike

    Member

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

Posted 07 April 2020 - 04:45 AM

The error suggests the user isn't being passed...

Although it's a long way around it, try

$arrapre = array('Name'=>''.$name.'');
$datapost = json_encode(''.$arrapre.'');


#32 maxt.v OFFLINE  

maxt.v

    Newbie

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

Posted 08 April 2020 - 01:53 AM

Thankyou SilvorMike, but is not working, I got the same result adding this code, I found a way using curl command, but is not what I want...



#33 msmcpeake OFFLINE  

msmcpeake

    Advanced Member

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

Posted 08 April 2020 - 12:04 PM

Here's my curl code I made just last week, hope it saves you some struggle!

 

$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, 'http://<Emby IP/hostname>:8096/emby/Users/New?api_key=<API Key>');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"Name\":\"{$name}\"}");

	$headers = array();
	$headers[] = 'Accept: application/json';
	$headers[] = 'Content-Type: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

	$result = curl_exec($ch);



	if (curl_errno($ch)) {
		echo 'Error:' . curl_error($ch);
	}
curl_close($ch);



$json = json_decode($result, true);


$userid = $json['Id'];


$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "http://<Emby IP/hostname>:8096/emby/Users/{$userid}/Connect/Link?api_key=<API Key>");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"ConnectUsername\":\"{$email}\"}");

	$headers = array();
	$headers[] = 'Accept: application/json';
	$headers[] = 'Content-Type: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

	$result2 = curl_exec($ch);
	if (curl_errno($ch)) {
		echo 'Error:' . curl_error($ch);
	}
curl_close($ch);

$json2 = json_decode($result2, true);

$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "http://<Emby IP/hostname>:8096/emby/Users/{$userid}/Policy?api_key=<API Key>");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"IsAdministrator\":false,\"IsHidden\":true,\"IsHiddenRemotely\":true,\"IsDisabled\":false,\"MaxParentalRating\":0,\"BlockedTags\":[\"\"],\"IsTagBlockingModeInclusive\":false,\"EnableUserPreferenceAccess\":true,\"AccessSchedules\":[\"\"],\"BlockUnratedItems\":[\"\"],\"EnableRemoteControlOfOtherUsers\":false,\"EnableSharedDeviceControl\":true,\"EnableRemoteAccess\":true,\"EnableLiveTvManagement\":true,\"EnableLiveTvAccess\":true,\"EnableMediaPlayback\":true,\"EnableAudioPlaybackTranscoding\":true,\"EnableVideoPlaybackTranscoding\":true,\"EnablePlaybackRemuxing\":true,\"EnableContentDeletion\":false,\"EnableContentDeletionFromFolders\":[\"\"],\"EnableContentDownloading\":false,\"EnableSubtitleDownloading\":false,\"EnableSubtitleManagement\":false,\"EnableSyncTranscoding\":true,\"EnableMediaConversion\":true,\"EnabledDevices\":[\"string\"],\"EnableAllDevices\":true,\"EnabledChannels\":[\"\"],\"EnableAllChannels\":true,\"EnabledFolders\":[\"\"],\"EnableAllFolders\":true,\"InvalidLoginAttemptCount\":0,\"EnablePublicSharing\":true,\"BlockedMediaFolders\":[\"\"],\"BlockedChannels\":[\"\"],\"RemoteClientBitrateLimit\":0,\"AuthenticationProviderId\":\"Emby.Server.Implementations.Library.DefaultAuthenticationProvider\",\"ExcludedSubFolders\":[\"\"],\"DisablePremiumFeatures\":false,\"SimultaneousStreamLimit\":0}");

	$headers = array();
	$headers[] = 'Accept: */*';
	$headers[] = 'Content-Type: application/json';
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

	$result4 = curl_exec($ch);
	if (curl_errno($ch)) {
		echo 'Error:' . curl_error($ch);
	}
curl_close($ch);

$json4 = json_decode($result4, true);

1st curl makes a new user, 2nd adds Emby Connect to that user, 3rd changes policies like hiding them from the login screen, etc.



#34 maxt.v OFFLINE  

maxt.v

    Newbie

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

Posted 11 April 2020 - 05:12 AM

It does, thank you!!



#35 symphonichorizon OFFLINE  

symphonichorizon

    Advanced Member

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

Posted 19 April 2020 - 09:00 PM

Just a heads up, the add user then add emby connect sometimes messes up and you will end up with a blank user, safer to do one or the other ( either make a local user or use emby connect. )



#36 msmcpeake OFFLINE  

msmcpeake

    Advanced Member

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

Posted 19 April 2020 - 09:08 PM

Just a heads up, the add user then add emby connect sometimes messes up and you will end up with a blank user, safer to do one or the other ( either make a local user or use emby connect. )

 

I haven't had this happen yet.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users