在函数内部创建的对象将不起作用,但传入的对象可以
Object made inside function won't work, but object being passed in does
使用 Google's Gmail API
,我在 listLabels
函数内的 Gmail object
中使用的模块 returns 和 auth
对象。当它传入时它工作得很好,但是如果我尝试在函数内部创建完全相同的对象并在 Gmail object
中使用它 returns 出这个(单独的 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
}
}
(两个令牌现已过期)
当登录验证时:
{ 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' }
您可能没有打印整个原型链。据我所知,console.log
默认情况下不会这样做。所以原来的 auth
有一些带有方法 request
的原型,而你的 "clone" 没有。因此错误。
或者您可能只是以某种方式在没有方法的情况下打印它,而 auth
直接具有方法 request
。但我认为不打印原型的可能性更大。
使用 Google's Gmail API
,我在 listLabels
函数内的 Gmail object
中使用的模块 returns 和 auth
对象。当它传入时它工作得很好,但是如果我尝试在函数内部创建完全相同的对象并在 Gmail object
中使用它 returns 出这个(单独的 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
}
}
(两个令牌现已过期)
当登录验证时:
{ 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' }
您可能没有打印整个原型链。据我所知,console.log
默认情况下不会这样做。所以原来的 auth
有一些带有方法 request
的原型,而你的 "clone" 没有。因此错误。
或者您可能只是以某种方式在没有方法的情况下打印它,而 auth
直接具有方法 request
。但我认为不打印原型的可能性更大。