【问题标题】:How to connect with 2ba it's API using PHP如何使用 PHP 连接 2ba 它的 API
【发布时间】:2021-05-05 21:53:55
【问题描述】:

我正在尝试连接到2ba 的 API 服务。不知怎的,我就是无法连接。我收到错误:错误:“invalid_client”

我不知道该尝试什么,感觉就像我需要散列我的 cliend_secret 或完整的 url 但我在文档中没有看到。

这是我的代码(PHP):

<?php

//  ---- GET TOKEN ----

// Base url for all api calls.
$baseURL = 'https://authorize.2ba.nl';

// Specified url endpoint. This comes after the baseUrl.
$endPoint = '/OAuth/Token';

// Parameters that are required or/and optianal for the endPoint its request.
$parameters = 'grant_type=password&username=abc@abc.com&password=123abc&client_id=myClientID&client_secret=myClientSecret';

// All parts together.
$url = $baseURL . $endPoint . '?' . $parameters;



//Init session for CURL.
$ch = curl_init();

// Options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// Init headers for access to the binance API signed data.
$headers = array();
$headers[] = 'Content-type: application/x-www-form-urlencoded';
$headers[] = 'Content-Length: 0';

// Setting headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute request.
$data = curl_exec($ch);

// If there is an error. Show whats wrong.
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

// Ends the CURL session, frees all resources and deletes the curl (ch).
curl_close($ch);

$result = json_encode($data);

echo($data);
exit();

?>

身份验证是 oauth2,我想使用“密码授予”流程,因为我可以通过这种方式自动登录。我还在 C# 中的 example code 中看到他们对 url 进行了编码,我还没有这样做但确实尝试过。它没有用。

// Using $encodedUrl like this: curl_setopt($ch, CURLOPT_URL, $encodedUrl); but does not work.
$encodedUrl = urlencode($url);

【问题讨论】:

    标签: php api oauth-2.0


    【解决方案1】:

    好的,我修好了。我现在获得了访问令牌,并且能够从 API 接收数据。这就是我所做的:

    //  ---- GET TOKEN - FLOW: USER PSW ----
    
    // No changes
    $baseURL = 'https://authorize.2ba.nl';
    
    // No changes
    $endPoint = '/OAuth/Token';
    
    // $parameters is now an array.
    $parameters = array(
        'grant_type'    => 'password',
        'username'      => 'myUsername',
        'password'      => 'myPassword',
        'client_id'     => 'myClientID',
        'client_secret' => 'myClientSecret'
    );
    
    // Removed the $parameter part
    $url = $baseURL . $endPoint;
    
    //Init session for CURL.
    $ch = curl_init();
    
    // Init headers for access to the binance API signed data.
    $headers = array();
    $headers['Content-Type'] = "application/x-www-form-urlencoded";
    
    // NOTE: http_build_query fixed it.
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); // Automaticly encodes parameters like client_secret and id.
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    // Execute request.
    $data = curl_exec($ch);
    
    // If there is an error. Show whats wrong.
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    
    // Ends the CURL session, frees all resources and deletes the curl (ch).
    curl_close($ch);
    
    $result = json_encode($data);
    
    echo($data);
    exit();
    

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多