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);
}
调用方法1
void Start()
{
StartCoroutine(GetAccessToken());
}
调用方法2
IEnumerator OtherFunction()
{
if (string.IsNullOrEmpty(TOKEN))
yield return GetAccessToken();
}