提问人:Impunkj 提问时间:7/25/2022 更新时间:7/25/2022 访问量:63
回调方法返回 Google People API 的未定义
Callback Method return undefined for Google people API
问:
我想在 API 中获取所有 Google 联系人数据。我正在使用两个函数来获取数据并在 API 中返回该数据。 API 名称 /contacts 我在联系人功能中获取未定义的数据。但是在 listconnection() 中,有所有数据。
/*
*
*
* @return JSON ARRAY
*/
app.get('/contacts', function(req, res){
// Load client secrets from a local file.
fs.readFile('credentials.json', async (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Tasks API.
let allcontact = authorize(JSON.parse(content), listConnectionNames);
console.log(allcontact);
return allcontact;
});
})
我正在使用另外两个函数来获取联系人中的数据。
/**
* Create an with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
return callback(oAuth2Client);
});
}
通过listconnection获取所有联系人。
/**
* Print the display name if available for 10 connections.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listConnectionNames(auth) {
const service = google.people({version: 'v1', auth});
service.people.connections.list({
resourceName: 'people/me',
pageSize: 50,
personFields: 'names,emailAddresses,phoneNumbers',
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
const connections = res.data.connections;
if (connections) {
return connections;
} else {
console.log('No connections found.');
}
});
}
如果有人可以帮助我
答: 暂无答案
评论