0
votes

I am working on a project that requires to oauth2 token to communicate. The backend gives me a curl command, but I have no idea how to put it into WWW form in Unity because I have no former experience with http or json file. Could you help me to access the token? Thanks. Here how the curl code looks like:

$ curl -v -u {CLIENT_ID}:{CLIENT_SECRET} "https://api.domo.com/oauth/token?grant_type=client_credentials&scope={SCOPE}"

and here is an example:

$ curl -v -u 441e307a-b2a1-4a99-8561-174e5b153fsa:f103fc453d08bdh049edc9a1913e3f5266447a06d1d2751258c89771fbcc8087 "https://api.domo.com/oauth/token?grant_type=client_credentials&scope=data%20user"

Thank you so much!

2
Common, just Google "Unity web request webform". You will see many results. Put some effort into doing this then ask a question when you are stuck.Programmer
Thanks Programmer, I have spent around 10 hours of figuring it out(Sorry I didn't have any experience with this...) The question I have is, I don't know what header should I put in, and what kind of data should I put in. Should I put the grant_type and scope as a header and putting client_id , secret as data..Stan X. Yang

2 Answers

0
votes

To get oauth2 token with the WWW API, use the WWWForm to create form that contains the grant_type, client_id and client_secret. When you make the request, the token should be returned in Json format. Use Unity's JsonUtility to convert it to string you can easily obtain.

Retrieving Token:

[Serializable]
public class TokenClassName
{
    public string access_token;
}

IEnumerator GetAccessToken()
{
    var url = "http://yoururl.com";
    var form = new WWWForm();
    form.AddField("grant_type", "client_credentials");
    form.AddField("client_id", "login-secret");
    form.AddField("client_secret", "secretpassword");

    WWW www = new WWW(url, form);

    //wait for request to complete
    yield return www;

    //and check for errors
    if (String.IsNullOrEmpty(www.error))
    {
        string resultContent = www.text;
        UnityEngine.Debug.Log(resultContent);
        TokenClassName json = JsonUtility.FromJson<TokenClassName>(resultContent);
        UnityEngine.Debug.Log("Token: " + json.access_token);
    }
    else
    {
        //something wrong!
        UnityEngine.Debug.Log("WWW Error: " + www.error);
    }
}

Using the Token:

After you receive the token, it will be stored in the json.access_token variable. You can use then use that to make requests by adding it to the Header of your other WWW requests. That header is stored in a Dictionary like this:

headers.Add("Authorization", "Bearer " + token);

Here is an full example for making a request with the received token:

IEnumerator makeRequestWithToken(string token)
{
    var url = "http://yoururl.com";

    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers.Add("Authorization", "Bearer " + token);

    WWWForm formData = new WWWForm();
    formData.AddField("YourField", "YourVal");
    formData.AddField("YourField", "YourVal");

    WWW www = new WWW(url, formData.data, headers);
    yield return www;

    //and check for errors
    if (String.IsNullOrEmpty(www.error))
    {
        string resultContent = www.text;
        UnityEngine.Debug.Log(resultContent);
    }
    else
    {
        //something wrong!
        UnityEngine.Debug.Log("WWW Error: " + www.error);
    }
}

Unity now uses UnityWebRequest. If possible start using that. Here is an example of retrieving oauth2 with UnityWebRequest.

Note that both functions are coroutine functions and must be started with the StartCoroutine function like StartCoroutine(GetAccessToken()); and StartCoroutine(makeRequestWithToken("YourToken"));.

0
votes

I got it working using the following method: Refer to Travis's fitbit code

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_clientID + ":" + _consumerSecret);  
var encoded = Convert.ToBase64String(plainTextBytes);  
var form = new WWWForm();  
form.AddField("client_id", _clientID);  
form.AddField("grant_type", "authorization_code");  
form.AddField("redirect_uri", WWW.UnEscapeURL(CallBackUrl));  
form.AddField("code", _returnCode);  
var headers = form.headers;  
headers["Authorization"] = "Basic " + encoded;  
_wwwRequest = new WWW("https://api.fitbit.com/oauth2/token", form.data, headers);  

Relate to your backend, see what kind of oauth2 they are using, you can see the oauth2 documentation Here. Feel free to leave a comment for question.