这是我使用https://github.com/php-twinfield/的库
当我打电话给Oauth登录时这是个问题。我已经用用户名和密码完成了几乎所有的API,但是客户端想要使用Oauth。我认为redirectUri存在一个问题。当我打电话给Oauth时,它总是显示:
{
"success": false,
"error": "invalid_grant"
}这是我的证件。客户端和客户端秘密从邮件中获得,重定向uri集从Openid Twinfield链接获得。如果证件有什么问题,请纠正我。
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;
}发布于 2018-06-14 05:45:24
首先,invalid_grant是一个标准的OAuth 2.0错误参数。由于OpenID连接构建在OAuth 2.0之上,因此接收此响应是有效的。如果你检查5.2错误响应部分,你会发现下面的解释
invalid_grant所提供的授权授予(例如授权代码、资源所有者凭据)或刷新令牌无效、过期、撤销、与授权请求中使用的重定向URI不匹配,或者是颁发给另一个客户端的。
正如它所解释的,它可以是从重定向URI、资源所有者凭据到的任何东西。但是我看到了一些与授权代码相关的代码问题。
// Here pass the authorization code
$accessToken = $provider->getAccessToken("authorization_code", ["code" =>'NLA000067']);您正在使用硬编码authorization_code (NLA000067)吗?这是错误的。授权代码授予的第一步是获得授权代码。然后,只有您可以执行令牌请求。您从授权请求中获得授权代码,而我认为您没有这样做。
如果是这样的话,您所得到的错误响应是完全有效的。如前所述,invalid_grant是由无效的授权代码造成的。
p.s-可能是这链接将指导您纠正问题
发布于 2019-02-12 14:16:37
@AnandPandey,按照下面的步骤
步骤1:
您首先需要构建要调用的url,以连接到Twinfield。为了做到这一点,您应该有如下所示的url。
https://login.twinfield.com/auth/authentication/connect/authorize?
client_id=Demorent
&client_secret=/iY7gyWn3Hkdgs4XzUG66SDyPNkk177x3A==
&redirect_uri=https://www.oauth.client.redirect.uri.com
&response_type=code
&force_login=0
&scope=openid+twf.user+twf.organisation+twf.organisationUser+offline_access
&state=STATELESS
&nonce=nonce注意:
1) redirect_uri需要与您在Twinfield注册的完全相同。
2)上面所示的作用域参数应该具有与上面给出的值相同的值。
3)验证您的client_id和client_secret
如果一切顺利,您将看到Twinflield登录页面,其中您需要使用您的凭据登录。成功登录后,您将被重定向到权限授予页面,以便基本上授予对应用程序访问Twinfield数据的访问权限。一旦单击“许可”,就会被重定向回使用授权代码指定的端点。
步骤2:
下一步是使用以下标头调用Twinfield accessTokenUri https://login.twinfield.com/auth/authentication/connect/token
header.add("code",authorizationCodeFromStep1);
header.add("redirect_uri", yourRegisteredRedirectUri);
header.add("grant_type", "authorization_code");
header.add("client_id", "Demorent");
header.add("client_secret", "/iY7gyWn3Hkdgs4XzUG66SDyPNkk177x3A==");如果所传递的所有参数都是正确的,您将得到一个使用id_token、accessToken、refreshToken、token_type和expires_in的响应。
https://stackoverflow.com/questions/50794466
复制相似问题