1. 程式人生 > >Unity學習筆記001.協程獲取Token

Unity學習筆記001.協程獲取Token

Unity學習筆記001.協程獲取Token

	IEnumerator GetAccessToken()
	{
		string url = string.Format("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}&", API_KEY, SECRECT_KEY);
        Debug.Log(url);

		UnityWebRequest
www = UnityWebRequest.Post(url, ""); yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } Debug.Log(www.downloadHandler.
text); TOKEN = GetJsonValue(www.downloadHandler.text, "access_token"); Debug.Log("TKOEN: " + TOKEN); //btnText.text = "TKOEN: " + TOKEN; }

呼叫方法1

	void Start()
	{
		StartCoroutine(GetAccessToken());
	}

呼叫方法2

		IEnumerator OtherFunction()
		{
			if (string.IsNullOrEmpty
(TOKEN)) yield return GetAccessToken(); }