1
votes

I am using yii2-authclient to authorize users and import google contact list

Steps I followed:

  1. Created Project in Google Console and enabled People API
  2. Setup config parameters, controllers, etc using docs. Tested Login and it worked fine. Configured it for contact redirect URI too.
  3. For Contacts, created a child class of \yii\authclient\clients\Google and made several tweaks:

    class Google extends \yii\authclient\clients\Google {
        /**
         * @var array list of attribute names, which should be requested from API to initialize contact list.
         */
        public $attributeNames = [
            'names',
            'emailAddresses',
        ];
    
        /**
         * Set base URL according for Contacts API
         */
        public function init() {
            parent::init();
            $this->apiBaseUrl = 'https://people.googleapis.com/v1';
            if ($this->scope === null) {
                $this->scope = implode(' ', [
                    'https://www.googleapis.com/auth/contacts',
                    'https://www.googleapis.com/auth/contacts.readonly',
                ]);
            }
        }
    
        /**
         * Call people.connection.list end point
         */
        protected function initUserAttributes() {
            return $this->api('people/me/connections', 'GET', [
                'personFields' => implode(',', $this->attributeNames),
                'pageSize' => 2000,
            ]);
        }
    }
    
  4. Inside a controller:

    public function actions() {
        return [
            'import' => [
                'class' => 'yii\authclient\AuthAction',
                'clientIdGetParamName' => 'authclient',
                'clientCollection' => '[collection_name_from_config]',
                'successCallback' => [$this, 'onImportSuccess'],
            ],
        ];
    }
    
    public function onImportSuccess($client) {
        ...
        $contacts = $client->getUserAttributes();
        ...
    }
    
1

1 Answers

0
votes

You might need to add the scope for basic profile information: https://www.googleapis.com/auth/userinfo.profile.

Scope list: https://developers.google.com/identity/protocols/googlescopes#peoplev1