1

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();
        ...
    }
    
skaur
  • 168
  • 2
  • 11

1 Answers1

0

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

Blake O'Hare
  • 1,863
  • 12
  • 16
  • Didn't work.. Plus, there is no mention of profile scope being required for reading contacts – skaur Feb 04 '18 at 04:43