【发布时间】:2018-02-04 00:21:57
【问题描述】:
我正在使用yii2-authclient授权用户并导入googlecontact list
我遵循的步骤:
- 在 Google 控制台中创建了项目并启用了 People API
- 使用docs 设置配置参数、控制器等。测试登录,它工作正常。也为联系人重定向 URI 配置了它。
-
对于联系人,创建了 \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, ]); } } -
在控制器内部:
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