在函数内部创建的对象不起作用,但传入的对象是

时间:2015-09-01 08:37:57

标签: javascript node.js oauth gmail-api

使用Google's Gmail API,模块返回我在auth函数内Gmail object内使用的listLabels对象。当它被传入时它工作正常,但是如果我尝试在函数内创建完全相同的对象并在Gmail object中使用它返回此(单独的GoogleAPIs模块)

error:     req = authClient.request(options, callback);
                     ^
TypeError: Object #<Object> has no method 'request'

这就是我现在的功能:

function listLabels(auth) {
    var auth1 = {
        "transporter": {},
        "clientId_": "75i4354355NOTID.apps.googleusercontent.com",
        "clientSecret_": "NOTSECRET",
        "redirectUri_": "http://notawebsite",
        "opts": {},
        "credentials": {
            "access_token": "not.not_access_token",
            "token_type": "Bearer",
            "expiry_data":1441095644613
        }

    }
        console.log("Original Auth: " + JSON.stringify(auth, null, 4));
        console.log("New Auth: " + JSON.stringify(auth1, null, 4));
        var gmail = google.gmail('v1');
        gmail.users.labels.list({
                auth: auth,
                userId: 'email@email.com',
        }, function(err, response) {
                if (err) {
                        console.log('The API returned an error: ' + err);
                        return;
                }
                var labels = response.labels;
                if (labels.length == 0) {
                        console.log('No labels found.');
                } else {
                        console.log('Labels:');
                        for (var i = 0; i < labels.length; i++) {
                                var label = labels[i];
                                console.log('- %s', label.name);
                        }
                }
        });
}

如果我使用传入的auth对象,它的工作正常,如果我使用auth1它不起作用并且给我上面的错误。

如您所见,我还尝试打印下面的两个对象:

Original Auth: {
    "transporter": {},
    "clientId_": "...",
    "clientSecret_": "...",
    "redirectUri_": "...",
    "opts": {},
    "credentials": {
        "access_token": "...",
        "token_type": "Bearer",
        "expiry_date": 1441098460931
    }
}
New Auth: {
    "transporter": {},
    "clientId_": "...",
    "clientSecret_": "...",
    "redirectUri_": "...",
    "opts": {},
    "credentials": {
        "access_token": "...",
        "token_type": "Bearer",
        "expiry_data": 1441095644613
    }
}

(两个令牌现已过期)

记录Auth时:

{ array: 
   [ { [Function: OAuth2Client]
       super_: [Function: AuthClient],
       GOOGLE_OAUTH2_AUTH_BASE_URL_: 'https://accounts.google.com/o/oauth2/auth',
       GOOGLE_OAUTH2_TOKEN_URL_: 'https://accounts.google.com/o/oauth2/token',
       GOOGLE_OAUTH2_REVOKE_URL_: 'https://accounts.google.com/o/oauth2/revoke',
       GOOGLE_OAUTH2_FEDERATED_SIGNON_CERTS_URL_: 'https://www.googleapis.com/oauth2/v1/certs',
       CLOCK_SKEW_SECS_: 300,
       MAX_TOKEN_LIFETIME_SECS_: 86400,
       ISSUER_: 'accounts.google.com' },
     [Function: AuthClient],
     [Function: Object] ],
  string: 'OAuth2Client :: AuthClient :: Object' }

1 个答案:

答案 0 :(得分:1)

可能你不打印整个原型链。据我所知,console.log默认情况下不会这样做。因此原始的auth有一些原型使用方法request,而你的&#34;克隆&#34;没有按&#39;吨。因此错误。

或许您只是简单地在没有方法的情况下打印它,auth直接使用方法request。但我认为不打印原型的可能性更大。