You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hangout API已废弃,如何不借助该API获取Hangout用户信息?

Alternative to Deprecated Hangout API for Fetching User/People Data

Hey there! Since the Hangout API has been fully deprecated, the official and supported replacement for accessing user profile information and people-related data is the Google People API. It's the modern, unified API for all Google user profile and contact operations. Let me show you how to refactor your existing code to use this API instead.

1. Frontend JavaScript (Replacing the gapi.client.plus.people.get Call)

The Google People API uses a slightly different request structure than the old Plus API. You'll need to target people/me as the resource name and explicitly specify the fields you want to retrieve (this is required by the API to optimize response size).

// First, load and initialize the Google People API client
gapi.load('client', () => {
  gapi.client.init({
    apiKey: 'YOUR_API_KEY', // Replace with your API key
    discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/people/v1/rest"],
  }).then(() => {
    // Fetch the authenticated user's profile data
    const request = gapi.client.people.people.get({
      'resourceName': 'people/me',
      'personFields': 'names,photos,urls' // Only request fields you need
    });
    
    request.execute(function(resp) {
      // Extract relevant data from the response
      const userDisplayName = resp.names[0].displayName;
      const userImageUrl = resp.photos[0].url;
      // Find the official profile URL (filter by URL type)
      const userProfileUrl = resp.urls.find(urlEntry => urlEntry.type === 'profile').value;
      
      console.log('Display Name: ' + userDisplayName);
      console.log('Image URL: ' + userImageUrl);
      console.log('Profile URL: ' + userProfileUrl);
    });
  });
});

For server-side PHP, you'll use the Google Client Library to interact with the People API. Note that the search method and response structure have changed—you'll need to use people_searchContacts (for personal contacts) or people_searchDirectoryPeople (for Google Workspace domain users) depending on your use case.

First, ensure you've installed the Google Client Library via Composer:

composer require google/apiclient:^2.0

Then, refactor your search code:

require __DIR__ . '/vendor/autoload.php';

// Initialize Google Client
$client = new Google\Client();
$client->setApplicationName('Your Application Name');
// Set appropriate scopes (adjust based on your needs)
$client->setScopes([
    Google\Service\People::CONTACTS_READONLY,
    Google\Service\People::USER_PROFILE_READ
]);
$client->setAuthConfig('credentials.json'); // Path to your OAuth 2.0 credentials file
$client->setAccessType('offline');

// Initialize People Service
$peopleService = new Google\Service\People($client);

// Configure search parameters
$optParams = [
    'query' => 'Brett',
    'pageSize' => 5,
    'personFields' => 'names,photos,urls' // Specify required fields
];

// Execute search (use people_searchDirectoryPeople for Workspace domain searches)
$results = $peopleService->people_searchContacts($optParams);

// Iterate through results
foreach ($results->getResults() as $result) {
    $person = $result->getPerson();
    $displayName = $person->getNames()[0]->getDisplayName();
    $resourceId = $person->getResourceName(); // Unique ID for the person
    
    print "ID: {$resourceId}\n<br>";
    print "Display Name: {$displayName}\n<br>";
    // Add more fields (like image URL or profile URL) as needed
}

Key Notes to Remember:

  • Enable the API: You must enable the Google People API in your Google Cloud Console before using it.
  • Scopes: Choose the minimal necessary scopes for your app to follow Google's best practices (e.g., profile for basic user data, contacts.readonly for searching contacts).
  • Field Selection: The People API requires you to explicitly list the fields you want to retrieve (via personFields), unlike the old Plus API which returned all fields by default.

内容的提问来源于stack exchange,提问作者Anand Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:54:08