在学习云函数的时候,自己按照API写案例,云端测试云函数没问题,本地测试云函数也没有问题,但是通过模拟器,在页面点击按钮的时候调用云函数就不行。
所有的配置都按照官网API中设置,希望知道原因,代码和截图如下:
直接新建了一个端云一体化项目,直接使用新建项目中原有的CloudFunction.ts做了简单修改:
CloudFunction.ts:
import cloud from '@hw-agconnect/cloud';
@Entry
@Component
struct CloudFunction {
build() {
Column() {
Navigation()
.title($r('app.string.cloud_function_title'))
Text($r('app.string.cloud_function_description'))
Button({ type: ButtonType.Normal }) {
Text($r('app.string.cloud_function_button_text'))
}.position({bottom:200})
.onClick(() => {
this.callIdGenerator();//-----------调用
})
}.height('100%')
}
private async callIdGenerator() {
console.log("开始调用...............")
try {
let funResult=await cloud.callFunction({
name:'idGenerator',
// name:'id-generator',
params:{
'aa':"aaa"
}
})
console.log('CloudFunction调用结果:-----',JSON.stringify(funResult))
}catch (e){
console.log("CloudFunction----ERR:---",JSON.stringify(e))
}
}
}
云函数idGenerator.ts,也是直接修改的项目创建时自动生成的示例模版:
let myHandler = async function (event, context, callback, logger) {
logger.info(`Input event: ${JSON.stringify(event)}`);
console.log("----start---")
callback({
code:200,
desc:"成功",
data:{
"a":"aaaa------",
"b":"bbbb------"
}
});
};
export { myHandler }
module.json5配置:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
]
oh-package.json5:
"dependencies": {
"@hw-agconnect/auth":"^1.0.0-beta",
"@hw-agconnect/hmcore": "^1.0.1",
"@hw-agconnect/cloud": "^1.0.1"
}
EntryAbility.ets:
async onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability
let file =await this.context.resourceManager.getRawFileContent('agconnect-services.json');
let json: string = buffer.from(file).toString();
auth.init(this.context, json);
//云函数初始化
initialize(this.context, JSON.parse(json));
}
这里曾尝试没有用异步函数,而是按照官网代码写成 getRawFileContentSync 方式,但是问题似乎并不是出在这里,无论怎么改始终无法调用成功
错误代码截图:
错误代码复制:
09-24 15:42:17.942 27017-27017 C0391e/AceGesture com.examp...20240924 I [click_recognizer.cpp(306)-(100000:100000:scope)] Click try accept
09-24 15:42:17.942 27017-27017 C03951/InputKeyFlow com.examp...20240924 I [click_recognizer.cpp(163)-(100000:100000:scope)] Click accepted, tag: Button
09-24 15:42:17.942 27017-27017 A03d00/JSAPP com.examp...20240924 I 开始调用...............
09-24 15:42:17.944 27017-27017 C03919/AceInputTracking com.examp...20240924 I [event_manager.cpp(2032)-(100000:100000:scope)] Consumed new event id=479 in ace_container, lastEventInfo: id:-1
09-24 15:42:17.954 27017-27017 A03d00/JSAPP com.examp...20240924 I cons
09-24 15:42:17.954 27017-27017 A0888f/AGC_StoredManager com.examp...20240924 I constructor, name is CN
09-24 15:42:17.954 27017-27017 A0888f/AGC_FakeSync com.examp...20240924 I create sync instance name: DefaultCrypto
09-24 15:42:17.954 27017-27017 A0888f/AGC_FakeSync com.examp...20240924 I DefaultCrypto instance initialing, start create
09-24 15:42:17.954 27017-27017 A0888f/AGC_DefaultAesCrypto com.examp...20240924 I create start
09-24 15:42:17.955 27017-27017 C01653/PreferencesJsKit com.examp...20240924 I GetContextMode: set gContextNode: 1, status: 0,
09-24 15:42:17.959 27017-27017 A00000/agc_aegis_crypto com.examp...20240924 I OhGenPbkdf2 start
09-24 15:42:17.960 27017-27017 A00000/agc_aegis_crypto com.examp...20240924 I OhGenPbkdf2 end
09-24 15:42:17.960 27017-27017 A0888f/AGC_DefaultAesCrypto com.examp...20240924 I create end
09-24 15:42:17.960 27017-27017 A0888f/AGC_FakeSync com.examp...20240924 I DefaultCrypto instance initialing, end create, callbacks length: 1
09-24 15:42:17.962 27017-27017 A0888f/AGC_FileStorage com.examp...20240924 I read file not existed.
09-24 15:42:17.962 27017-27017 A0888f/AGC_HTTP com.examp...20240924 I --->POST, region:CN,interface:cnagcapigwwisefunctionfunctions24ofz3kjidGenerator-$latest serviceInfo:agconnect-cloud/1.0.0
09-24 15:42:17.963 27017-27143 C01653/NativePreferences com.examp...20240924 W LoadFromDisk: The settingXml ohos_agc_credential_aegis load failed.
09-24 15:42:17.964 27017-27017 A0888f/AGC_AesCrypto com.examp...20240924 I cipher is empty:
09-24 15:42:17.964 27017-27017 A0888f/AGC_HTTP com.examp...20240924 I --->POST, region:CN,interface:cnagcapigwoauth2v1token serviceInfo:agconnect-credential/
09-24 15:42:17.964 27017-27037 C057c6/BinderInvoker com.examp...20240924 I BinderInvoker 91: created invoker 4386992
09-24 15:42:17.977 27017-27017 C01120/BMS com.examp...20240924 E [bundle_mgr_proxy.cpp(GetBigString:4735)]host reply err 8521232
09-24 15:42:17.977 27017-27017 C015b0/NetConnManager com.examp...20240924 E [network_security_config.cpp:322]Get json failed.
09-24 15:42:17.977 27017-27017 C015b0/NetConnManager com.examp...20240924 E [network_security_config.cpp:68]GetConfig failed
09-24 15:42:18.018 27017-27112 C01719/ffrt com.examp...20240924 W 120:~WorkerThread:65 WorkerThread enter destruction
09-24 15:42:18.024 27017-27113 C01719/ffrt com.examp...20240924 W 124:~WorkerThread:65 WorkerThread enter destruction
09-24 15:42:18.161 27017-27144 C015b0/NETSTACK com.examp...20240924 I [http_exec.cpp:456] taskid=-2147483648, size:46, dns:16.814, connect:47.582, tls:59.626, firstSend:0.811, firstRecv:0.000, total:182.261, redirect:0.000, errCode:0, RespCode:400, httpVer:2, method:POST
09-24 15:42:18.162 27017-27017 A0888f/AGC_HTTP com.examp...20240924 I <---POST, region:CN,interface:cnagcapigwoauth2v1token serviceInfo:agconnect-credential/ responseCode:400, responseMsg:undefined, constTime:198
09-24 15:42:18.163 27017-27017 A0888f/AGC_HTTP com.examp...20240924 I <---POST, region:CN,interface:cnagcapigwwisefunctionfunctions24ofz3kjidGenerator-$latest serviceInfo:agconnect-cloud/1.0.0 responseCode:400, responseMsg:, constTime:201
09-24 15:42:18.163 27017-27017 A03d00/JSAPP com.examp...20240924 I CloudFunction----ERR:--- {"code":400}
09-24 15:42:22.946 27017-27111 C01719/ffrt com.examp...20240924 W 128:~WorkerThread:65 WorkerThread enter destruction
09-24 15:42:22.946 27017-27111 C057c6/BinderInvoker com.examp...20240924 I ~BinderInvoker 96: destroyed invoker 13238416
查错的过程中发现云函数并没有被调用到,以为是函数名错误,但是把能想到的都测试了一遍还是调用不成功
这个问题做一下记录,我查找了很多天,每天都把想到可能有问题的地方做了调整,最后发现问题出现在配置SDK的时候,下载的SDK打开了一个开关,虽然打开开关后,也配置了秘钥信息,但是就是调用不成功,关掉这个不包含秘钥开关后就调用云函数成功了。