1

我正在使用 yii2-authclient 授权用户并导入谷歌联系人列表

我遵循的步骤:

  1. 在 Google Console 中创建项目并启用 People API
  2. 使用docs设置配置参数、控制器等。测试登录,它工作正常。也将其配置为联系人重定向 URI。
  3. 对于联系人,创建了 \yii\authclient\clients\Google 的子类并进行了一些调整:

    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. 在控制器内部:

    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();
        ...
    }
    
4

1 回答 1

0

您可能需要添加基本配置文件信息的范围:https://www.googleapis.com/auth/userinfo.profile.

范围列表:https ://developers.google.com/identity/protocols/googlescopes#peoplev1

于 2018-02-04T03:48:01.783 回答