importpathfrom'node:path';importprocessfrom'node:process';import{authenticate}from'@google-cloud/local-auth';import{google}from'googleapis';// The scope for reading contacts.constSCOPES=['https://www.googleapis.com/auth/contacts.readonly'];// The path to the credentials file.constCREDENTIALS_PATH=path.join(process.cwd(),'credentials.json');/** * Prints the display names of the first 10 connections. */asyncfunctionlistConnectionNames(){// Authenticate with Google and get an authorized client.constauth=awaitauthenticate({scopes:SCOPES,keyfilePath:CREDENTIALS_PATH,});// Create a new People API client.constservice=google.people({version:'v1',auth});// Get the list of connections.constresult=awaitservice.people.connections.list({resourceName:'people/me',pageSize:10,personFields:'names,emailAddresses',});constconnections=result.data.connections;if(!connections||connections.length===0){console.log('No connections found.');return;}console.log('Connections:');// Print the display name of each connection.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.');}});}awaitlistConnectionNames();
运行示例
在工作目录中,运行示例:
node .
首次运行该示例时,系统会提示您授权访问:
如果您尚未登录 Google 账号,请在系统提示时登录。如果您登录了多个账号,请选择一个账号用于授权。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-09-16。"],[[["\u003cp\u003eThis quickstart provides instructions for creating a Node.js command-line application that interacts with the Google People API.\u003c/p\u003e\n"],["\u003cp\u003eThe application uses client libraries for authentication, suitable for testing environments, while production environments require more robust authorization methods.\u003c/p\u003e\n"],["\u003cp\u003eTo 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.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves enabling the API, configuring OAuth consent, authorizing credentials, installing libraries, setting up the sample code, and running the application.\u003c/p\u003e\n"],["\u003cp\u003eOnce authorized, the application retrieves and displays the names of up to 10 connections from the People API.\u003c/p\u003e\n"]]],["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"],null,[]]