我正在尝试向 M365 的(“me/todo/lists”)端点发出 GET 请求。它最初在新的身份验证和获取访问令牌后工作。
它甚至可以在保存访问令牌并在几分钟后发出请求时工作,这意味着使用保存的访问令牌的代码有效。
但是,当我尝试通过存储访问令牌来调用 API 时,出现错误:“错误:访问令牌已过期或尚未生效”
这是使用保存的访问令牌发出请求的代码
public async getTaskLists(accessToken: string) {
class MyAuthenticationProvider implements AuthenticationProvider {
/**
* This method will get called before every request to the msgraph server
* This should return a Promise that resolves to an accessToken (in case of success) or rejects with error (in case of failure)
* Basically this method will contain the implementation for getting and refreshing accessTokens
*/
public async getAccessToken(): Promise<string> {
return accessToken;
}
}
const options = {
authProvider: new MyAuthenticationProvider(), // An instance created from previous step
};
const client = Client.initWithMiddleware(options);
//this part fetches task-lists from M365 and populates them into placed in a tree-view using registerTreeDataProvider
let dataObject = [];
let children: string[] = [];
try {
let taskList = await client.api("/me/todo/lists").get();
console.log(taskList);
} catch (error) {
console.log(error);
}
}
}