【问题标题】:Coinbase Invalid 404 request OAuth2 GuzzleCoinbase 无效的 404 请求 OAuth2 Guzzle
【发布时间】:2020-08-12 04:50:18
【问题描述】:

我尝试使用 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);
        }

在 coinbase 授权后,我重定向到重定向 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',
            'client_id' => $this->clientId,
            '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', $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);
        }

    }
}

我也在邮递员那里检查过,他回复很好: enter image description here

【问题讨论】:

  • 不要让你自己的coinbase客户端使用包!

标签: php laravel oauth-2.0 guzzle coinbase-api


【解决方案1】:

问题在于 URL 访问令牌,需要使用 https://api.coinbase.com/oauth/token 而不是 http://www.coinbase.com/oauth/token

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 2018-10-22
    • 2019-01-12
    • 2015-04-21
    • 2012-06-08
    • 2019-01-26
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多