如何解决Coinbase无效404请求OAuth2异常
我尝试使用OAuth2在coinbase中进行授权:
$client = new Client(['cookies' => true]);
try {
$response = $client->request('POST',$this->urlAccessToken,[
'headers' => [
'cache-control' => 'no-cache','Content-Type' => 'application/x-www-form-urlencoded'
],'form_params' => [
'grant_type' => 'authorization_code','code' => $request->code,'client_id' => $this->clientId,'client_secret' => $this->clientSecret,'redirect_uri' => $this->redirectUri
]
]);
dd($response->getBody());
} catch (\Exception $e) {
return response($e->getMessage(),400);
}
在硬币库中获得授权后,我重定向到重定向URI,并且在发送请求交换代码时看到错误的响应:
客户端错误:
POST http://www.coinbase.com/oauth/token
导致404 Not Found
响应:无效的请求。代替GET请求,您应该使用有效的POST参数进行POST。有关更多信息(截断...)
将在Coinbase中授权的所有代码:
private $clientId;
private $clientSecret;
private $redirectUri;
private $urlAuthorize;
private $urlAccessToken;
public function __construct()
{
$this->clientId = env('COINBASE_CLIENT_ID');
$this->clientSecret = env('COINBASE_CLIENT_SECRET');
$this->redirectUri = route('oauth2-redirect');
$this->urlAuthorize = 'https://www.coinbase.com/oauth/authorize';
$this->urlAccessToken = 'http://www.coinbase.com/oauth/token';
}
public function oauth(Request $request)
{
$state = hash('sha256',$request->session()->getId());
if (!isset($request->code)) {
$parameters = [
'response_type' => 'code','redirect_uri' => $this->redirectUri,'state' => $state
];
$authorizationUrl = $this->urlAuthorize . '?' . http_build_query($parameters);
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
} elseif (empty($request->state) || $request->state !== $state) {
return response('Invalid state',400);
} else {
$client = new Client(['cookies' => true]);
try {
$response = $client->request('POST',400);
}
}
}
我也检查了邮递员,他得到了很好的答复: enter image description here
解决方法
问题出在URL访问令牌中,需要使用https://api.coinbase.com/oauth/token
代替http://www.coinbase.com/oauth/token
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。