错误403:使用Google People Api访问用户联系人时返回的范围不足

时间:2018-02-04 00:21:58

标签: oauth-2.0 yii2 google-people yii2-authclient

我正在使用yii2-authclient授权用户并导入Google contact list

我遵循的步骤:

  1. 在Google控制台中创建了Project并启用了People API
  2. 使用docs设置配置参数,控制器等。测试登录,它工作正常。为联系人重定向URI配置了它。
  3. 对于Contacts,创建了一个子类\ 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();
        ...
    }
    

1 个答案:

答案 0 :(得分:0)

您可能需要添加基本个人资料信息的范围:https://www.googleapis.com/auth/userinfo.profile

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