步骤:
1、获取token
2、调用api,以json形式传参
详解:
第一步:获取token
相关文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
第二步:
相关文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html
第三步:获取token后,
IEnumerator testq(byte[] b)
{
WWWForm form = new WWWForm(); //不需要form来传参
string json = "{"type":"云函数方法名"}";
UnityWebRequest webRequest = UnityWebRequest.Post("https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=“第一步获取到的token”&env=“云环境的envId”&name=“云环境已经部署的云函数名”", form);
webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return webRequest.SendWebRequest();
if (webRequest.isHttpError || webRequest.isNetworkError)
{
Debug.Log(webRequest.error);
}
else
{
print(webRequest.downloadHandler.text) ;
}
}
网友评论