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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...