[英]How to use new microsoft graph api to log users in to yii site
I'm creating a site that needs an oauth authorization through microsoft.我正在创建一个需要通过微软进行 oauth 授权的网站。 In yii/authclient there's only live client and it is not working anymore.在 yii/authclient 中只有实时客户端,它不再工作了。
I tried to write my own but something goes wrong.我试图自己写,但出了点问题。 As far as I understood my AuthAction doesn't see clientId and returns 404 exception without text.据我了解,我的 AuthAction 没有看到 clientId 并返回没有文本的 404 异常。 Here's my code of the auth client.这是我的身份验证客户端代码。
What I get我得到的
AuthAction class method run (it's default) AuthAction 类方法运行(默认)
class Office365OAuth extends OAuth2
{
public $authUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
public $tokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
public $apiBaseUrl = 'https://login.microsoftonline.com/common/oauth2/v1.0';
public $scope = null;
public function init()
{
parent::init();
if ($this->scope === null)
{
$this->scope = 'https://graph.microsoft.com/User.Read';
}
}
/**
* Overrides default function to fix malformed url
*/
public function getReturnUrl()
{
return $this->returnUrl;
}
protected function defaultName()
{
return 'office365';
}
protected function defaultTitle()
{
return 'Office365';
}
/**
* For popup mode
*/
protected function defaultViewOptions()
{
return [
'popupWidth' => 800,
'popupHeight' => 500,
];
}
/**
* Gets new auth token to replace expired one.
*/
protected function initUserAttributes()
{
return $this->api('me', 'GET');
}
}
So, how can I authenticate through MS graph?那么,如何通过 MS 图进行身份验证?
The yii\\authclient
package requires using the returnUrl having a request param authclient=live
, eg https://example.com/site/auth?authclient=live
yii\\authclient
包需要使用具有请求参数authclient=live
,例如https://example.com/site/auth?authclient=live
However, Azure prohibits request params in the returnUrl
.但是,Azure 禁止在returnUrl
使用请求参数。 Therefore, to make yii\\authclient
works with Azure, ie, the returnUrl as https://example.com/site/auth/live
.因此,要使yii\\authclient
与 Azure 一起使用,即 returnUrl 为https://example.com/site/auth/live
。 You need to prettify the url with request param as follows:您需要使用请求参数美化网址,如下所示:
In config/main.php
在config/main.php
'components' => [
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'rules' => [
'site/auth/<authclient>' => 'site/auth'
]
]
]
In controllers/SiteController.php
,在controllers/SiteController.php
,
public function actions()
{
return [
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'onAuthSuccess']
]
];
}
...
public function onAuthSuccess($client) {
// get user data from client
$userAttributes = $client->getUserAttributes();
// DO YOUR THING
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.