我正在开发 VSCode 扩展。部分功能是从 MS Graph 获取 TodoTaskList。我目前正在使用 TypeScript 版本 4.3.5 并使用 https://github.com/microsoftgraph/msgraph-typescript-typings/、https://github.com/microsoftgraph/msgraph-sdk-javascript 上的文档#2-create-a-client-instance 和 https://learn.microsoft.com/en-us/graph/api/resources/todo-overview?view=graph-rest-1.0
我能够使用 VS Code 身份验证 API 获取 accessToken,这是我编写的用于获取 TodoTaskList 的代码。我在 extension.ts 中导入 getMyTasks 并将 accessToken 传递到那里。
import { Client } from "@microsoft/microsoft-graph-client";
import { AuthenticationProvider } from "@microsoft/microsoft-graph-client";
require('isomorphic-fetch');
import * as MicrosoftGraph from "@microsoft/microsoft-graph-types";
export async function getMyTasks(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);
try {
let taskList = await client.api("/me/todo/lists").get();
let tasks: [MicrosoftGraph.TodoTaskList] = taskList.value;
for (let task of tasks) {
console.log(task.displayName);
}
} catch (error) {
throw error;
}
}
这是我的 package.json 依赖项:
"devDependencies": {
"@microsoft/microsoft-graph-types": "^1.41.0",
"@types/glob": "^7.1.3",
"@types/mocha": "^8.0.0",
"@types/node": "^14.0.27",
"@types/vscode": "^1.53.0",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"eslint": "^7.9.0",
"glob": "^7.1.6",
"mocha": "^8.1.3",
"typescript": "^4.0.2",
"vscode-test": "^1.4.0"
},
"dependencies": {
"@microsoft/microsoft-graph-client": "^2.2.1",
"isomorphic-fetch": "^3.0.0"
}
此问题已在此处确认:github.com/microsoftgraph/msgraph-sdk-javascript/issues/124
修复:tsconfig.json 中需要
lib: ["es6", "dom"]
非常感谢@yume-chan
为缺失的类型设置全局类型将解决问题
declare global {type RequestInfo = string | Request};