I am trying to get content from BIM 360. I have the access token but this will not return what it returns in Postman.
What am I doing wrong?
UnityWebRequest get_hub = UnityWebRequest.Get("https://xxxxxx/contents");
get_hub.SetRequestHeader("Authorization", accessToken.text.ToString());
get_hub.SetRequestHeader("Content-Type", "application/json");
yield return get_hub.SendWebRequest();
if (!get_hub.isNetworkError)
{
string resultContent = get_hub.downloadHandler.text;
HUBClassName json = JsonUtility.FromJson<HUBClassName>(resultContent);
Debug.Log(json.hubData);
HUB_Data.text = json.hubData;
Debug.Log("Test = Bearer " + accessToken.text.ToString());
}
else
{
Debug.Log("Not working");
}
The auth header payload might need to be mime64 or something… best way to test is to wire in a proxy, fire off your curl/postman and see what it looks like, then fire off from Unity and compare.
Might also just be some other missing header, or perhaps escaping of something, or the auth might be supplied as an argument in the URL.
Instead of “Not working” you might want to display what the error code is. Your actions for a 404 would be quite different than a 403 or a 500 for instance.
@Kurt-Dekker
I did, it’s just not in there. It comes back as null as well. In Postman I go to the URL with it set to Get and with the access token it works fine. Am I setting up the access token the correct way?
Double check the actual auth header that Postman is sending. It may need to be “Authorization: Bearer ”. In code that would be like: get_hub.SetRequestHeader("Authorization", "Bearer " + accessToken.text.ToString());
This is assuming it’s Bearer Authentication: Bearer Authentication
As @Kurt-Dekker mentioned you’ll probably learn a lot if you inspect the network error you’re getting a bit.
@PraetorBlue , I’m not getting a network error.
it would be helpful to share what you are getting.
This is it.
Null
UnityEngine.Debug:Log(Object)
I even typed the URL wrong to see if it threw an error and it did…
Do you think I need to wait for all the content before trying to display it?
Have you tried printing out the raw json string?
Debug.Log("raw json: " + resultContent);
1 Like
That worked. I got all the json data. Why is it working for the access token but not for this?
It sounds like your “HUBClassName” class is not structured properly to deserialize your json data into.
1 Like
I’ll try and see what’s up. But I structured it the same way I did for the access token.
Thank you.
Ooooh, man, didn’t see this: I would highly recommend NOT using the Unity micro-tiny JSON implementation, just stay away from it. It is extremely limited and fails to handle many common types, including Dictionaries (!!!).
Instead, get JSON .NET from the asset store for free… it is FAR more robust. That alone might just solve the problem, assuming a good POCO and valid JSON.
1 Like