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

使用PHP调用Google People API创建联系人时遇Scope权限不足错误

Fixing "Insufficient Authentication Scopes" Error When Creating Contacts with Google People API (PHP)

Let's break down the issues in your code and fix the permission error step by step.

1. The Core Problem: Scope & Cached OAuth Tokens

Even though you added Google_Service_PeopleService::CONTACTS, this error usually happens because:

  • Your application was previously authorized with a narrower scope, and the old OAuth token is still cached. Google doesn't automatically update scopes without re-authorization.
  • There's a small typo in your phone number class reference that might be complicating things once the scope issue is fixed.

2. Step-by-Step Fixes

a. Refresh Authorization with the Correct Scope

First, confirm you're using the right scope for creating contacts. Google_Service_PeopleService::CONTACTS (which maps to https://www.googleapis.com/auth/contacts) is sufficient for full read/write access to contacts. However:

  • Delete any cached OAuth token files (like token.json if you're using the default file storage in your getClient() method). This forces your app to re-request authorization with the correct scope.
  • Double-check that your getClient() method isn't overriding the scope you're adding later—make sure it doesn't set a more restrictive scope by default.

b. Fix the Phone Number Class Typo

In your code, you're using Google_Service_People_PhoneNumber but the correct class name is Google_Service_PeopleService_PhoneNumber (matching the service namespace you're using). This typo would throw a class-not-found error once you resolve the scope issue.

c. Corrected Code

Here's the revised version of your code with all fixes applied:

$client = getClient();
// Ensure we're using the full contacts read/write scope
$client->addScope(Google_Service_PeopleService::CONTACTS);
$service = new Google_Service_PeopleService($client);

$person = new Google_Service_PeopleService_Person();

// Add email address (note: expects an array of objects)
$email = new Google_Service_PeopleService_EmailAddress();
$email->setValue('test@test.com');
$person->setEmailAddresses([$email]);

// Add display name (expects an array of objects)
$name = new Google_Service_PeopleService_Name();
$name->setDisplayName('User de Test');
$person->setNames([$name]);

// Add phone number (fixed class name + added value)
$phoneNumber = new Google_Service_PeopleService_PhoneNumber();
$phoneNumber->setType('Home');
$phoneNumber->setValue('+1234567890'); // Don't forget to set the actual phone number!
$person->setPhoneNumbers([$phoneNumber]);

// Create the contact with error handling
try {
    $exe = $service->people->createContact($person);
    print_r($exe);
} catch (Google_Service_Exception $e) {
    echo "Error creating contact: " . $e->getMessage();
}

d. Key Additional Notes

  • When setting properties like emailAddresses, names, or phoneNumbers, the API expects an array of objects, not a single object. Your original code passed a single object, which could cause unexpected behavior even after fixing the scope.
  • Double-check that you've enabled the Google People API in your Google Cloud Console project—it's an easy step to overlook!

3. Verify Authorization

After deleting the cached token, run your script again. It should redirect you to the Google authorization page, where you'll need to grant access to your contacts. Once authorized, the new token will include the correct scope, and the "insufficient scopes" error should disappear.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:33:12