How to create a rest authentication header in unity
Hey,
So I am trying to link https://avwx.rest/ to my project. I have implemented the API fine and it works as expected, the only issue I have is that I have no idea where to place the authentication script. Here is my code:
public class METARScript : MonoBehaviour
{
private readonly string AVWXURL = "https://private-anon-f687e87c21-avwx.apiary-mock.com/api/metar/egss";
private void Start()
{
StartCoroutine(loadMETAR());
}
IEnumerator loadMETAR()
{
UnityWebRequest METARInfoRequest = UnityWebRequest.Get(AVWXURL);
yield return METARInfoRequest.SendWebRequest();
if(METARInfoRequest.isNetworkError || METARInfoRequest.isHttpError)
{
Debug.Log(METARInfoRequest.error);
yield break;
}
JSONNode METARInfo = JSON.Parse(METARInfoRequest.downloadHandler.text);
string METAR = METARInfo["sanitized"];
print("The METAR for EGSS is: " + METAR);
}
}
I am trying to add the authentication key: --stripped-- which will give permission to access the data on the server.
Can anyone shed any light on how to do this?
Thanks
Answer by Bunny83 · Feb 16, 2020 at 03:23 PM
Well, just have a look at the documentation of that service. There are generally two ways you can provide your API key / token. First is inside an Authorization header the second is through a get url parameter. If you want to use an header, just follow the instructions. You can use any prefix or just the key itself. So they are actually quite flexible on the server side.
So this is the first part, to know how the API expects the key. Second is look up how you can set a request header when using a UnityWebRequest. So the first point would be the documentation. You will notice that the UnityWebRequest has a method called SetRequestHeader. The usage should be straight forward. So just do
METARInfoRequest.SetRequestHeader("Authorization", yourAPIKey);
or like in the example on the API documentation page you can also add a prefix like:
METARInfoRequest.SetRequestHeader("Authorization", "TOKEN " + yourAPIKey);
Of course this line has to be executed before you call "SendWebRequest()".
Alternatively you can just pass it as a url parameter
UnityWebRequest METARInfoRequest = UnityWebRequest.Get(AVWXURL + "?token="+yourAPIKey);
Excellent. Thank you :) Turns out my issues actually stemmed from me getting confused and using the wrong API URL. Thank you for helping me with this :)
it works well on postman when i try on unity it not working. It looks like the token header was stripped :( below is my code:
#if UNITY_ANDROID || UNITY_IOS
WWWForm form = new WWWForm();
form.AddField("id", id);
form.AddField("idPlatform", idplatform);
UnityWebRequest www = UnityWebRequest.Post("url", form);
www.SetRequestHeader("token", token);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
callback("null");
}
else
{
callback(www.downloadHandler.text);
}
#endif
@Bunny83 I have a question about this issue. If I want to add data in my database example ingame coins and specify a key so that no one can falsify the data etc.. , someone could see the key in the code, so if you decompile the game. I'm afraid that if I send my data, for example ingame coins or something, to my website, then someone else could do the same and cheat something like that.
Your answer
Follow this Question
Related Questions
How to handle UnityWebRequest Errors when throws Curl 28 Error 1 Answer
MultipartFormDataSection not working - Error: 403 0 Answers
Enemy AI for Multiplayer (using NavMeshAgent) unity 5.6.5f1 0 Answers
,Call api and wait answer to return (async?) 1 Answer
Using unity to collect information from a non-unity application 0 Answers