Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>百度智能云度厂版New to Visual Studio Code? Get it now.
百度智能云度厂版

百度智能云度厂版

Baidu Internal

|
5,831 installs
| (0) | Free
提供百度智能云度厂版的账号登录
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

百度智能云(度厂版)登录插件

本插件注册了一个 PROVIDER_ID 为 baidu-int-cloud-uuap-account 的 VS Code AuthenticationProvider。你可以通过 VS Code 插件 API vscode.authentication.getSession 获取当前登录的用户在 Conosle Hub 上的身份信息,进而访问所有云上百度的接口。

下面是一个获取 session 函数的示例:

import * as vscode from 'vscode';

const PROVIDER_ID = 'baidu-int-cloud-uuap-account';

// 缓存用户 token,防止每次都调用 getSession
let currentSession: vscode.AuthenticationSession | undefined = undefined;

// 每次 session change 的时候(登录或者退出登录时触发)
vscode.authentication.onDidChangeSessions(async e => {
    if (e.provider.id === PROVIDER_ID) {
        // 获取当前登录的用户信息,更新缓存
        const session = await vscode.authentication.getSession(PROVIDER_ID, [], {
            // 静默获取当前的状态,其实不太理解为啥 VS Code 不直接在事件对象里面把 session 直接给我
            createIfNone: false,
        });
        currentSession = session;
    }
});

// 获取当前登录的用户信息
export async function getSession() {
    if (currentSession) {
        return currentSession;
    }
    const session = await vscode.authentication.getSession(PROVIDER_ID, [], {
        // 如果没有就登录
        createIfNone: true,
    });
    currentSession = session;
    return currentSession;
}

session 的结构如下:

/**
 * Represents a session of a currently logged in user.
 */
export interface AuthenticationSession {
    /**
     * The identifier of the authentication session.
     * 对于我们的 provider,这里对应 uuap 用户名,如 zhangsan
     */
    readonly id: string;

    /**
     * The access token.
     * 对于我们的 provider,这里对应登录使用的 uuap jwt token,下面有使用方法
     */
    readonly accessToken: string;

    /**
     * The account associated with the session.
     */
    readonly account: {
        /**
         * The unique identifier of the account.
         * 对于我们的 provider,这里对应 uuap 用户名,如 zhangsan
         */
        readonly id: string;

        /**
         * The human-readable name of the account.
         * 对于我们的 provider,这里对应 uuap 用户名,如 张三
         */
        readonly label: string;
    };

    /**
     * The permissions granted by the session's access token. Available scopes
     * are defined by the {@link AuthenticationProvider}.
     * 因为我们没有说不同操作对应不同的 token,所以是空数组
     */
    readonly scopes: readonly string[];
}

下面是 session 中的 accessToken 也就是 uuap jwt token 的使用方法:

/**
 * console 用户登录信息的验证
 *
 * @param consoleToken
 * @returns
 */
export const getConsoleUserLogin = async (consoleToken: string) => {
    const response = await axios.get<any>(
        'https://console.cloud.baidu-int.com/api/xxx',
        {
            headers: {
                'uuap-authorization': consoleToken,
            },
        }
    );
    const data = response.data;
    // do what you want...
};

另外,为了保证插件初始化顺序合理,如果是强依赖本插件的用户信息的话,可以在 package.json 中添加 extensionDependencies,如下:

"extensionDependencies": [
    "baidu-int.console-cloud-account"
]
  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft