D
D
dmitryIG2019-08-21 14:27:16
PHP
dmitryIG, 2019-08-21 14:27:16

Tinkoff API. How to get access_token using guzzle?

Good day, I'm trying to log in via guzzle to the RKO Tinkoff Bank API.
By default, everything works via CURL:

$params=['grant_type'=>'refresh_token',
   'refresh_token'=>'<token>'
];
$headers = [
'POST /secure/token HTTP/1.1',
   'Content-Type: application/x-www-form-urlencoded'
];
$curlURL='https://sso.tinkoff.ru/secure/token';       
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$curlURL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$curl_res = curl_exec($ch);
if($curl_res) {
    $server_output = json_decode($curl_res);
}
var_dump($curl_res);

I'm trying to rewrite the code with guzzle and getting 400 Bad Request:
$refresh_token = '<token>';

$client = new GuzzleHttp\Client(['headers' => ['Content-Type' => 'application/x-www-form-urlencoded']]);

$response = $client->request('POST', 'https://sso.tinkoff.ru/secure/token', [
    'query' => ['grant_type' => 'refresh_token','refresh_token' => $refresh_token]
]);

echo $response->getStatusCode();
echo $response->getBody();

I can not understand what the problem is, I tried different options. Anyone have similar experience? Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmitryIG, 2019-08-21
@Blitzzz

Here is the working code

$response = $client->request('POST', 'https://sso.tinkoff.ru/secure/token', [
    'form_params' => [
        'grant_type' => 'refresh_token',
        'refresh_token' => $refresh_token
    ]
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question