【问题标题】:Issues with using Microsoft Graph with typescript使用带有 typescript 的 Microsoft Graph 的问题
【发布时间】:2021-07-20 14:08:59
【问题描述】:

我正在开发一个 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-instancehttps://docs.microsoft.com/en-us/graph/api/resources/todo-overview?view=graph-rest-1.0 的文档

我可以使用 VS Code Authentication 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"
    }

我收到以下错误:

【问题讨论】:

标签: typescript microsoft-graph-api vscode-extensions


【解决方案1】:

在此确认此问题:github.com/microsoftgraph/msgraph-sdk-javascript/issues/124

修复:在 tsconfig.json 中需要 lib: ["es6", "dom"]

非常感谢@yume-chan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2020-07-30
    相关资源
    最近更新 更多