constfs=require('fs').promises;constpath=require('path');constprocess=require('process');const{authenticate}=require('@google-cloud/local-auth');const{google}=require('googleapis');// If modifying these scopes, delete token.json.constSCOPES=['https://www.googleapis.com/auth/contacts.readonly'];// The file token.json stores the user's access and refresh tokens, and is// created automatically when the authorization flow completes for the first// time.constTOKEN_PATH=path.join(process.cwd(),'token.json');constCREDENTIALS_PATH=path.join(process.cwd(),'credentials.json');/** * Reads previously authorized credentials from the save file. * * @return {Promise<OAuth2Client|null>} */asyncfunctionloadSavedCredentialsIfExist(){try{constcontent=awaitfs.readFile(TOKEN_PATH);constcredentials=JSON.parse(content);returngoogle.auth.fromJSON(credentials);}catch(err){returnnull;}}/** * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise<void>} */asyncfunctionsaveCredentials(client){constcontent=awaitfs.readFile(CREDENTIALS_PATH);constkeys=JSON.parse(content);constkey=keys.installed||keys.web;constpayload=JSON.stringify({type:'authorized_user',client_id:key.client_id,client_secret:key.client_secret,refresh_token:client.credentials.refresh_token,});awaitfs.writeFile(TOKEN_PATH,payload);}/** * Load or request or authorization to call APIs. * */asyncfunctionauthorize(){letclient=awaitloadSavedCredentialsIfExist();if(client){returnclient;}client=awaitauthenticate({scopes:SCOPES,keyfilePath:CREDENTIALS_PATH,});if(client.credentials){awaitsaveCredentials(client);}returnclient;}/** * Print the display name if available for 10 connections. * * @param {google.auth.OAuth2} auth An authorized OAuth2 client. */asyncfunctionlistConnectionNames(auth){constservice=google.people({version:'v1',auth});constres=awaitservice.people.connections.list({resourceName:'people/me',pageSize:10,personFields:'names,emailAddresses',});constconnections=res.data.connections;if(!connections||connections.length===0){console.log('No connections found.');return;}console.log('Connections:');connections.forEach((person)=>{if(person.names && person.names.length > 0){console.log(person.names[0].displayName);}else{console.log('No display name found for connection.');}});}authorize().then(listConnectionNames).catch(console.error);
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[[["This quickstart provides instructions for creating a Node.js command-line application that interacts with the Google People API."],["The application uses client libraries for authentication, suitable for testing environments, while production environments require more robust authorization methods."],["To run the quickstart, you'll need Node.js, a Google Cloud project, a Workspace domain with API access, and a Google Account with admin privileges."],["The process involves enabling the API, configuring OAuth consent, authorizing credentials, installing libraries, setting up the sample code, and running the application."],["Once authorized, the application retrieves and displays the names of up to 10 connections from the People API."]]],["This guide details how to create a Node.js application to interact with the Google People API using quickstarts. The key actions include: enabling the People API in a Google Cloud project, configuring the OAuth consent screen, creating OAuth 2.0 client credentials, and installing the necessary client libraries via npm. A sample `index.js` file is provided to read contact information. The application requires authorization upon its initial run, storing credentials for subsequent uses. Finally, to run the code you must use the command `node .`.\n"]]