Created
November 25, 2021 20:24
-
-
Save cptangry/8996b29348d32e83d680ed0324e0d7fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Networking; | |
using System.Collections; | |
[[Serializable] | |
public class TokenClassName | |
{ | |
public string access_token; | |
} | |
private static IEnumerator GetAccessToken(Action<string> result) | |
{ | |
Dictionary<string, string> content = new Dictionary<string, string>(); | |
//Fill key and value | |
content.Add("grant_type", "client_credentials"); | |
content.Add("client_id", "login-secret"); | |
content.Add("client_secret", "secretpassword"); | |
UnityWebRequest www = UnityWebRequest.Post("https://api.home-connect.com/api/homeappliances/{haId}/programs/active -H 'Authorization: Bearer {access_token}'", content); | |
//Send request | |
yield return www.Send(); | |
if (!www.isError) | |
{ | |
string resultContent = www.downloadHandler.text; | |
TokenClassName json = JsonUtility.FromJson<TokenClassName>(resultContent); | |
//Return result | |
result(json.access_token); | |
} | |
else | |
{ | |
//Return null | |
result(""); | |
} | |
} | |
public class BenimDatam : BenimDatam | |
{ | |
void Start() | |
{ | |
StartCoroutine(Upload()); | |
} | |
IEnumerator Upload() | |
{ | |
WWWForm secenekler_degerleri = new WWWForm(); | |
WWWForm secenekler = new WWWForm(); | |
WWWForm veri = new WWWForm(); | |
WWWForm form = new WWWForm(); | |
secenekler_degerleri .AddField("ConsumerProducts.CoffeeMaker.Option.BeanAmount", "ConsumerProducts.CoffeeMaker.EnumType.BeanAmount.Strong"); | |
secenekler_degerleri .AddField("ConsumerProducts.CoffeeMaker.Option.FillQuantity", 35); | |
secenekler.AddField("options", secenekler_degerleri); | |
veri.AddField("ConsumerProducts.CoffeeMaker.Program.Beverage.Espresso", secenekler); | |
form.AddField("data", veri); | |
using (UnityWebRequest www = UnityWebRequest.Post("https://api.home-connect.com/api/homeappliances/{haId}/programs/active -H 'Content-Type: application/vnd.bsh.sdk.v1+json'", form)) | |
{ | |
yield return www.SendWebRequest(); | |
string token = null; | |
yield return GetAccessToken((tokenResult) => { token = tokenResult; }); | |
www.SetRequestHeader("Authorization", "Bearer " + token); | |
www.Send(); | |
if (www.result != UnityWebRequest.Result.Success) | |
{ | |
Debug.Log(www.error); | |
} | |
else | |
{ | |
Debug.Log("Form upload complete!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment