npm 包中未定义的"this"



我正在尝试通过OAuth2向Google进行身份验证,并且我正在使用googleapis NPM软件包。为此,我创建了一个小型本地包以在我的项目中使用:

包装.js

Npm.depends({
  "googleapis": "2.0.1"
});
Package.describe({
  name: 'google-tokens',
  summary: 'Library for requesting Google API tokens',
  version: '0.0.1'
});
Package.onUse(function (api) {
  api.use(['underscore', 'http', 'service-configuration', 'oauth', 'accounts-oauth'], ['client', 'server']);
  api.addFiles('lib/tokens_client.js', 'client');
  api.addFiles('lib/tokens_server.js', 'server');
  api.export(['Tokens']);
});

tokens_server.js(包装)

Tokens = (function() {
  Google       = Npm.require('googleapis');
  OAuth2Client = Google.auth.OAuth2;
  var my       = {};
  my.fetchTokens = function(code) {
    var clientId     = Meteor.settings.google.CLIENT_ID;
    var clientSecret = Meteor.settings.google.CLIENT_SECRET;
    var redirect     = Meteor.settings.google.REDIRECT_URL;
    oauth2Client = OAuth2Client(clientId, clientSecret, redirect);
    var response = Async.runSync(function(done) {
      oauth2Client.getToken(code, function(error, tokens) {
        done(error, tokens);
      });
    });
    console.log(response);
    return response;
  };
  ....

但是,上面的代码返回

Exception while invoking method 'addChannel' TypeError: Cannot set property 'transporter' of undefined
I20150316-23:33:03.520(1)?     at AuthClient (/Users/madsh/Programming/Projects/youtubr/packages/google-tokens/.npm/package/node_modules/googleapis/node_modules/google-auth-library/lib/auth/authclient.js:25:20)
I20150316-23:33:03.520(1)?     at OAuth2Client (/Users/madsh/Programming/Projects/youtubr/packages/google-tokens/.npm/package/node_modules/googleapis/node_modules/google-auth-library/lib/auth/oauth2client.js:38:23)
I20150316-23:33:03.520(1)?     at Object.my.fetchTokens (packages/google-tokens/lib/tokens_server.js:11:1)

它指的是 .npm/package/node_modules/googleapis 文件夹中node_modules文件夹中的这部分,其中包含以下内容

'use strict';
var DefaultTransporter = require('../transporters.js');
/**
 * Base class for authentication clients.
 */
function AuthClient() {
  this.transporter = new DefaultTransporter(); <---------- Failing line
}
/**
 * Export AuthClient.
 */
module.exports = AuthClient;

我不知道为什么会这样,有什么建议吗?

您应该将身份验证客户端创建为实例,即

var client = new Oauth2Client(...

相关内容

  • 没有找到相关文章

最新更新