php – 使用Twinfield Openid Oauth连接时的“Invalid_grant”响应
作者:互联网
这是我用过https://github.com/php-twinfield/的图书馆
当我调用Oauth登录时,这是一个问题.我已经使用用户名和密码完成了几乎所有API,但客户希望使用Oauth.我认为redirectUri存在问题.当我打电话给Oauth时,它总是显示:
{
"success": false,
"error": "invalid_grant"
}
这是我的凭证. Clientid和clientsecret是从邮件中获取的,并且是从Openid Twinfield链接中设置的重定向uri.如果凭证有任何问题,请纠正我.
clientId : Demorent
clientSecret : /iY7gyWn3Hkdgs4XzUG66SDyPNkk177x3A==
redirectUri : https://www.oauth.client.redirect.uri.com
使用的代码:
public function login(\Illuminate\Http\Request $request)
{
try {
// In the $request param all the credential given
$provider = new \PhpTwinfield\Secure\Provider\OAuthProvider([
'clientId' => $request->clientId,
'clientSecret' => $request->clientSecret,
'redirectUri' => $request->redirectUri
]);
// Here pass the authorization code
$accessToken = $provider->getAccessToken("authorization_code", ["code" =>'NLA000067']);
$refreshToken = $accessToken->getRefreshToken();
$office = \PhpTwinfield\Office::fromCode("1008");
$connection = new \PhpTwinfield\Secure\OpenIdConnectAuthentication($provider, $refreshToken, $office);
$customerApiConnector = new \PhpTwinfield\ApiConnectors\CustomerApiConnector($connection);
$result = $customerApiConnector->get('1008',$office);
$jsonResponse = JsonResponse::success($result);
} catch(SoapFault $e) {
$jsonResponse = empty($e->getMessage()) ? JsonResponse::error(class_basename($e)) : JsonResponse::error($e->getMessage());
}
return $jsonResponse;
}
解决方法:
首先,invalid_grant是标准的OAuth 2.0错误参数.由于OpenID Connect是基于OAuth 2.0构建的,因此接收此响应是有效的.如果您查看5.2 Error Response section,则会发现以下说明
invalid_grant
The provided authorization grant (e.g., authorization
code, resource owner credentials) or refresh token is
invalid, expired, revoked, does not match the redirection
URI used in the authorization request, or was issued to
another client.
正如它解释的那样,它可以是重定向URI,资源所有者凭证.但是我发现您的代码与授权代码有关.
// Here pass the authorization code
$accessToken = $provider->getAccessToken("authorization_code", ["code" =>'NLA000067']);
您使用的是硬编码授权码(NLA000067)吗?这是错的.授权代码授权的第一步是获取授权代码.然后,只有您可以执行令牌请求.您从授权请求中获取授权代码,我不认为您这样做.
如果是这种情况,您获得的错误响应完全有效.如上所述,invalid_grant是由无效授权代码产生的.
p.s-可能是this链接将指导您纠正问题
标签:openid-connect,php,lumen-5-4,twinfield 来源: https://codeday.me/bug/20190910/1799740.html