Jump to content

Help with using api from unity


ginjaninja

Recommended Posts

ginjaninja

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

ginjaninja

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);
Link to comment
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...