我正在使用yii2-authclient授权用户并导入Google contact list
我遵循的步骤:
对于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,
]);
}
}
在控制器内:
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();
...
}
答案 0 :(得分:0)
您可能需要添加基本个人资料信息的范围:https://www.googleapis.com/auth/userinfo.profile
。
范围列表:https://developers.google.com/identity/protocols/googlescopes#peoplev1