我在浏览器中使用Google JavaScript API,并且间歇性地失败并出现以下异常:gapi.client.Error: API discovery response missing required fields
。可能导致这种情况的原因是什么?
我加载API的代码如下所示,我基本上是将一个脚本节点注入到DOM中以加载API,将整个东西包装在Promise链中以便处理它的时间'准备好了:
new Promise((resolve, reject) => {
const script = document.createElement('script')
script.onload = resolve
script.onerror = reject
document.currentScript.parentNode.insertBefore(script, document.currentScript)
script.src = 'https://apis.google.com/js/api.js'
})
.then(() => {
return new Promise((resolve) => {
gapi.load('client:auth2', resolve)
})
})
.then(() => {
return gapi.client.init({
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest',],
apiKey: initialState.googleApiKey,
clientId: initialState.oauthClientId,
scope: 'email profile https://www.googleapis.com/auth/calendar',
})
})
回溯如下:
gapi.client.Error: API discovery response missing required fields.
at new wY (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:653:491)
at f (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:696:457)
at h.o0 (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:79:171)
at xs (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:82:245)
at Wq (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:82:96)
at null._.C.uea (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:81:242)
at Ap (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_US.nyHIlPOzvxo.O/m=auth2,client/rt=j/sv=1/d=1/ed=1/am=AQ/rs=AGLTcCNWHYGSUbRcGfoOSnVtpSAoqp5S-Q/cb=gapi.loaded_0:75:300)