Jump to content


Photo

Help with using api from unity

unity api

  • Please log in to reply
3 replies to this topic

#1 ginjaninja OFFLINE  

ginjaninja

    Advanced Member

  • Members
  • 1634 posts
  • Local time: 10:55 AM
  • Locationuk

Posted 13 June 2019 - 11:08 AM

Can anyone help with this please?

I am learning Unity and the emby api, and diving in well past the deep end..

I am getting a 400 bad response when trying to use "/emby/Users/AuthenticateByName"

 

when i manually use the same headers and json data on the http://swagger.emby.media page, i get a successful response.

 

from that i understand

i need to set a header and post some json data

 

i can get a successful response in Unity from simpler api methods like /System/Info which dont require posting json and setting headers.

anyone out there got a unity app to work on the api? that could a post a unity example on using /emby/Users/AuthenticateByName sucessfully (or something similar with headers and json?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class EmbyAPIComp : MonoBehaviour
{

  
  [System.Serializable]
  public class MyClass
  {
      string Username = "MyUsername";
      string Pw = "Mypassword";
  }

  static MyClass myObject = new MyClass();

  string json = JsonUtility.ToJson(myObject);


 IEnumerator AuthenticateCoroutine()
  {
     using (UnityWebRequest www = UnityWebRequest.Put("http://192.168.0.10:8096/emby/Users/AuthenticateByName", json))
      {
      www.method = UnityWebRequest.kHttpVerbPOST;
      www.SetRequestHeader("X-Emby-Authorization" , "Emby UserId='91d851f075f44ae59bc499bc4ebb54e6', Client='Android', Device='Samsung Galaxy SIII', DeviceId='NickskApp', Version='1.0.0.0'");
      www.SetRequestHeader("accept" , "application/json");
      www.SetRequestHeader("Content-Type" , "application/json");

      yield return www.SendWebRequest();

      if(www.isNetworkError || www.isHttpError)
      {
          Debug.Log(www.error);
      }
      else
      {
          Debug.Log(www.responseCode);
      }
    }
  }

thanks for your help



#2 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 136138 posts
  • Local time: 05:55 AM

Posted 13 June 2019 - 12:37 PM

For authentication you wouldn't put the userid in the header because shouldn't have a user id at that point. try removing that.



#3 ginjaninja OFFLINE  

ginjaninja

    Advanced Member

  • Members
  • 1634 posts
  • Local time: 10:55 AM
  • Locationuk

Posted 14 June 2019 - 04:32 AM

thanks luke, thats good to know the inital authentication doesnt require the id.

 

turned out i was not instancing the json object properly.

 

for anyone else getting started this worked, now getting 200 response.

 

 

[System.Serializable]
  public class MyClass
  {
      public string Username;
      public string Pw;

      public MyClass()
      {
        this.Username = "MyUsername";
        this.Pw = "MyPassword";
      }
  }

  static MyClass myObject = new MyClass();

  string json = JsonUtility.ToJson(myObject);


#4 Luke OFFLINE  

Luke

    System Architect

  • Administrators
  • 136138 posts
  • Local time: 05:55 AM

Posted 14 June 2019 - 11:44 AM

Thanks for the info.





Also tagged with one or more of these keywords: unity, api

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users