【问题标题】:Error 403: Insufficient Scope returned while using Google People Api for accessing user contacts错误 403:使用 Google People Api 访问用户联系人时返回的范围不足
【发布时间】:2018-02-04 00:21:57
【问题描述】:

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

我遵循的步骤:

  1. 在 Google 控制台中创建了项目并启用了 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();
        ...
    }
    

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 没用。另外,没有提到读取联系人需要配置文件范围
    猜你喜欢
    • 2021-09-30
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多