K
K
kamwork2021-05-23 18:00:49
API
kamwork, 2021-05-23 18:00:49

Integration with CDEK 2.0 API - why can't I get a token via GuzzleHttp (everything works in postman)?

I'm trying to integrate with the CDEK API https://confluence.cdek.ru/pages/viewpage.action?p... and get a Token for work.

Through postman everything works, I try through GuzzleHttp, it returns an error:

Client error: `POST https://api.cdek.ru/v2/oauth/token` resulted in a `401 Unauthorized` response:
{"timestamp":"2021-05-23T14:57:42.291+00:00","status":401,"error":"Unauthorized","message":"","path":"/oauth/token"}
"""


The class looks like this:
<?php

namespace App\Helpers;
use GuzzleHttp\Client;

class CDEK2
{
    public function __construct() {
        $this->client_id = settings('cdek_account')->value;
        $this->client_secret = settings('cdek_password')->value;

        $this->cdek = new Client();
    }

    public function getToken() {
        $token = $this->cdek->request('POST', 'https://api.cdek.ru/v2/oauth/token', [
            'headers' => ['Content-type' => 'application/x-www-form-urlencoded'],
            'parameters' => [
                'grant_type' => 'client_credentials',
                'client_id'  => $this->client_id,
                'client_secret' => $this->client_secret
                ]
        ]);

        return $token;
    }

    public function getCityId()
    {
        return $this->getToken();
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kamwork, 2021-05-23
@kamwork

Understood.

$response = $this->cdek->request('POST', 'https://api.cdek.ru/v2/oauth/token?parameters', [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id'  => $this->client_id,
                'client_secret' => $this->client_secret
                ]
        ]);

         var_dump($response->getBody()->getContents());

J
JhaoDa, 2021-05-23
@JhaoDa

We must take the official SDK 2.0 and not suffer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question