以下代码为response.accessToken
生成以下TypeScript错误:
import * as Msal from '@azure/msal-browser'
export async function getTokenPopup(request: Msal.TokenExchangeParameters) {
return await auth.acquireTokenSilent(request).catch(async () => {
return await auth.acquireTokenPopup(request).catch((error) => {
console.log('login with popup failed: ', error)
})
})
}
const getGraphDetails = async (
uri: string,
scopes: Msal.TokenExchangeParameters,
axiosConfig?: AxiosRequestConfig
) => {
return getTokenPopup(scopes).then((response) => {
return callGraph(uri, response.accessToken, axiosConfig)
})
}
在检查TokenResponse的TS definition时,它清楚地说明对象上的属性accessToken
可用:
export type TokenResponse = AuthResponse & {
uniqueId: string;
tenantId: string;
scopes: Array<string>;
tokenType: string;
idToken: string;
idTokenClaims: StringDict;
accessToken: string;
refreshToken: string;
expiresOn: Date;
account: Account;
};
我在做什么错了?
答案 0 :(得分:1)
尽管您使用的是Error: Expected a value of type 'FirebaseError', but got one of type 'DomException'
at Object.throw_ [as throw] (http://localhost:44987/dart_sdk.js:4461:11)
at Object.castError (http://localhost:44987/dart_sdk.js:4432:15)
at Object.cast [as as] (http://localhost:44987/dart_sdk.js:4748:17)
at dart.AnonymousJSType.new.as (http://localhost:44987/dart_sdk.js:6186:64)
at handleThenable (http://localhost:44987/packages/firebase/src/storage.dart.lib.js:3264:96)
at handleThenable.throw (<anonymous>)
at http://localhost:44987/dart_sdk.js:36909:38
at _RootZone.runBinary (http://localhost:44987/dart_sdk.js:36762:58)
at _FutureListener.thenAwait.handleError (http://localhost:44987/dart_sdk.js:31933:48)
at handleError (http://localhost:44987/dart_sdk.js:32475:51)
at Function._propagateToListeners (http://localhost:44987/dart_sdk.js:32498:17)
at _Future.new.[_completeError] (http://localhost:44987/dart_sdk.js:32354:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:44987/dart_sdk.js:32392:31)
at Object._microtaskLoop (http://localhost:44987/dart_sdk.js:37015:13)
at _startMicrotaskLoop (http://localhost:44987/dart_sdk.js:37021:13)
,但请注意您的catch
。我会这样写这段代码:
await
现在,为什么在import * as Msal from '@azure/msal-browser'
export async function getTokenPopup(request: Msal.TokenExchangeParameters) {
try {
return await auth.acquireTokenSilent(request);
} catch (error) {
return await auth.acquireTokenPopup(request);
}
}
const getGraphDetails = async (
uri: string,
scopes: Msal.TokenExchangeParameters,
axiosConfig?: AxiosRequestConfig
) => {
try {
const response = await getTokenPopup(scopes);
return callGraph(uri, response.accessToken, axiosConfig);
} catch (error) {
throw new Error("You could not get a token");
}
}
中得到void
。函数response
对于getTokenPopup
和acquireTokenSilent
都可能失败。因此,函数acquireTokenPopup
将引发错误(或不返回任何内容,具体取决于您的实现)。
TypeScript看到了这一点,并添加了getTokenPopup
类型,以表明有可能无法获得响应。