同步执行异步 NPM 方法 w 回调

此示例将异步方法 oauth2.client.getToken(callback) 从包 NPM 包 simple-oauth2in 包装到 Fiber 中,以便可以同步调用该方法。

const oauth2 = require('simple-oauth2')(credentials);

const credentials = {
    clientID: '#####',
    clientSecret: '#####',
    site: "API Endpoint Here."
};

Meteor.startup(() => {
    let token = Meteor.wrapAsync(oauth2.client.getToken)({});
    if (token) {
        let headers = {
            'Content-Type': "application/json",
            'Authorization': `Bearer ${token.access_token}`
        }

        // Make use of requested OAuth2 Token Here (Meteor HTTP.get).
    }
});