无法解构属性“ OAuth2”的“未定义”或“空”

时间:2019-11-20 03:48:27

标签: meteor

我正在尝试在 CentOS 7 上运行rocket.chat。但这会引发异常:

I20191120-09:44:42.581(7)? Exception in callback of async function: TypeError: Cannot destructure property `OAuth2` of 'undefined' or 'null'.
I20191120-09:44:42.582(7)?     at routes.js (app/livestream/server/routes.js:8:27)
I20191120-09:44:42.582(7)?     at fileEvaluate (packages/modules-runtime.js:336:7)
I20191120-09:44:42.582(7)?     at Module.require (packages/modules-runtime.js:238:14)
I20191120-09:44:42.582(7)?     at Module.moduleLink [as link] (/root/.meteor/packages/modules/.0.13.0.1ny9ohg.lqq5++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:38:38)
I20191120-09:44:42.582(7)?     at index.js (app/livestream/server/index.js:1:8)
I20191120-09:44:42.583(7)?     at fileEvaluate (packages/modules-runtime.js:336:7)
I20191120-09:44:42.583(7)?     at Module.require (packages/modules-runtime.js:238:14)
I20191120-09:44:42.583(7)?     at Module.moduleLink [as link] (/root/.meteor/packages/modules/.0.13.0.1ny9ohg.lqq5++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:38:38)
I20191120-09:44:42.583(7)?     at importPackages.js (server/importPackages.js:1:1667)
I20191120-09:44:42.584(7)?     at fileEvaluate (packages/modules-runtime.js:336:7)
I20191120-09:44:42.584(7)?     at Module.require (packages/modules-runtime.js:238:14)
I20191120-09:44:42.584(7)?     at Module.moduleLink [as link] (/root/.meteor/packages/modules/.0.13.0.1ny9ohg.lqq5++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/reify/lib/runtime/index.js:38:38)
I20191120-09:44:42.584(7)?     at main.js (server/main.js:1:8)
I20191120-09:44:42.584(7)?     at fileEvaluate (packages/modules-runtime.js:336:7)
I20191120-09:44:42.585(7)?     at Module.require (packages/modules-runtime.js:238:14)
I20191120-09:44:42.585(7)?     at require (packages/modules-runtime.js:258:21)
I20191120-09:44:42.585(7)?     at /usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/app/app.js:145189:15
I20191120-09:44:42.585(7)?     at /usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/boot.js:419:36
I20191120-09:44:42.585(7)?     at Array.forEach (<anonymous>)
I20191120-09:44:42.586(7)?     at /usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/boot.js:228:19
I20191120-09:44:42.586(7)?     at /usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/boot.js:479:5
I20191120-09:44:42.586(7)?     at Function.run (/usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/profile.js:510:12)
I20191120-09:44:42.586(7)?     at /usr/app_chat/Rocket.Chat/.meteor/local/build/programs/server/boot.js:478:11
import { Meteor } from 'meteor/meteor';
import google from 'googleapis';

import { settings } from '../../settings';
import { Users } from '../../models';
import { API } from '../../api';

const { OAuth2 } = google.auth;

API.v1.addRoute('livestream/oauth', {
    get: function functionName() {
        const clientAuth = new OAuth2(settings.get('Broadcasting_client_id'), settings.get('Broadcasting_client_secret'), `${ settings.get('Site_Url') }/api/v1/livestream/oauth/callback`.replace(/\/{2}api/g, '/api'));
        const { userId } = this.queryParams;
        const url = clientAuth.generateAuthUrl({
            access_type: 'offline',
            scope: ['https://www.googleapis.com/auth/youtube'],
            state: JSON.stringify({
                userId,
            }),
        });

        return {
            statusCode: 302,
            headers: {
                Location: url,
            },
            body: 'Oauth redirect',
        };
    },
});

API.v1.addRoute('livestream/oauth/callback', {
    get: function functionName() {
        const { code, state } = this.queryParams;

        const { userId } = JSON.parse(state);

        const clientAuth = new OAuth2(settings.get('Broadcasting_client_id'), settings.get('Broadcasting_client_secret'), `${ settings.get('Site_Url') }/api/v1/livestream/oauth/callback`.replace(/\/{2}api/g, '/api'));

        const ret = Meteor.wrapAsync(clientAuth.getToken.bind(clientAuth))(code);

        Users.update({ _id: userId }, { $set: {
            'settings.livestream': ret,
        } });

        return {
            headers: {
                'content-type': 'text/html',
            },
            body: '<script>window.close()</script>',
        };
    },
});

1 个答案:

答案 0 :(得分:0)

就像已经说明的https://stackoverflow.com/users/7080548/sebastian-kaczmarek,您需要替换:

import google from 'googleapis';

使用

import { google } from 'googleapis';

另请参阅 https://www.npmjs.com/package/googleapis#first-example