我正在为浏览器使用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)