我在使用Microsoft Graph的Slim3 WebApp中使用联盟/ oauth客户端...而且我无法通过给定的刷新令牌成功请求新令牌...
这是我的联盟/ oauth-client的di-container:
$container['oauthprovider'] = function ()
{
$provider = new \League\OAuth2\Client\Provider\GenericProvider(
[
'clientId' => getenv('ENV_CLIENT_ID'),
'clientSecret' => getenv('ENV_CLIENT_SECRET'),
'redirectUri' => getenv('ENV_REDIRECT_URL'),
'urlAuthorize' => getenv('ENV_AUTHORIZE_URL'),
'urlAccessToken' => getenv('ENV_URL_ACCESS_TOKEN'),
'urlResourceOwnerDetails' => '',
'scopes' => 'offline_access user.read people.read user.read.all openid mail.send'
]
);
return $provider;
};
工作正常 - 在我的Slim3-Route重定向之后,我得到了所有令牌,refresh_token等。
$provider = $this -> oauthprovider;
$CODE = filter_var($_GET['code'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$accessToken = $provider -> getAccessToken('authorization_code',
['code' => $CODE]
);
...
$existingAccessToken = $accessToken -> getToken();
$refreshToken = $accessToken -> getRefreshToken();
$expiresIn = $accessToken -> getExpires();
$expired = ($accessToken -> hasExpired() ? true : false);
$client = $this -> guzzzle;
...
如果我在此时尝试刷新我的令牌 - 在同一个Slim3路线中 - 它工作正常:
$newAccessToken = $provider -> getAccessToken('refresh_token', [
'refresh_token' => $accessToken -> getRefreshToken(),
'grant_type' => 'refresh_token'
]);
但这不是我的目标 - 我必须通过达到过期的生命 - 1小时 - 在另一个Slim3-Route中刷新令牌:
$accessToken = unserialize($session -> get('serialized_token'));
$refreshToken = unserialize($session -> get('serialized_refresh_token'));
$code = $session -> get('code');
$provider = $this -> oauthprovider;
$client = $this -> guzzzle;
...
$req = $client -> request('POST',
'https://login.windows.net/common/oauth2/token', [
'form_params' => [
'accept' => 'application/json',
'grant_type'=> 'refresh_token',
'client_id' => getenv('ENV_CLIENT_ID'),
'client_secret' => getenv('ENV_CLIENT_SECRET'),
'refresh_token' => (string) $refreshToken,
'redirect_uri' => getenv('ENV_REDIRECT_URL')
]
]);
回应:
客户端错误:
POST https://login.windows.net/common/oauth2/token
导致400 Bad Request
响应:{“error”:“invalid_grant”,“error_description”:“AADSTS70000:传输数据解析器失败:刷新令牌格式错误(截断.. 。)
刷新令牌与我在初始请求中获得的完全相同。
任何有oauth-client / guzzle / Microsoft Graph经验的人 - 我的错误是什么/哪里?
答案 0 :(得分:1)
由于您使用的是v2端点,因此POST
应为https://login.microsoftonline.com/common/oauth2/v2.0/token
,并且您的有效负载应包含scope
属性:
$req = $client -> request('POST',
'https://login.microsoftonline.com/common/oauth2/v2.0/token', [
'form_params' => [
'grant_type'=> 'refresh_token',
'client_id' => getenv('ENV_CLIENT_ID'),
'client_secret' => getenv('ENV_CLIENT_SECRET'),
'refresh_token' => (string) $refreshToken,
'redirect_uri' => getenv('ENV_REDIRECT_URL'),
'scope' => 'offline_access user.read people.read user.read.all openid mail.send'
]
]);
答案 1 :(得分:0)
例如 - 如果我使用我的联盟/ oauth-Client请求Microsoft-Graph并通过以下方式请求:
$request = $client -> request('GET', "https://login.microsoftonline.com/common/oauth2/v2.0/token", [
'form_params' => [
'accept' => 'application/json',
'grant_type'=> 'refresh_token',
'client_id' => getenv('ENV_CLIENT_ID'),
'client_secret' => getenv('ENV_CLIENT_SECRET'),
'refresh_token' => (string) $refreshToken,
'redirect_uri' => getenv('ENV_REDIRECT_URL')
]
]);
$response = json_decode($request -> getBody() -> getContents(), true);
echo 'Response: ';
var_dump($response);
exit;
回复包含以下信息:
Response: array(7) {
["token_type"]=>
string(6) "Bearer"
["scope"]=>
string(45) "Mail.Send People.Read User.Read User.Read.All"
["expires_in"]=>
int(3599)
["ext_expires_in"]=>
int(0)
["access_token"]=>
string(1901) "...f8SQPrPFsg66q8vHLGM4Q..."
["refresh_token"]=>
string(847) "...cEksGS9XfHIqTH2LUYL..."
["id_token"]=>
string(928) "...KKWAUtlyS0p5rDWILr..."
}
有了这些信息,我可以续订我的应用程序令牌和刷新令牌,并继续请求Microsoft-Graph端点。
谢谢你,Marc!太好了!