如何使用最新的(alpha)版本的FreshBooks API获取访问令牌

时间:2017-03-28 06:25:18

标签: php freshbooks-api

我想获得一个访问令牌。我使用了以下代码,它与最新版本的FreshBooks的文档相同,但它给了我一个错误。

if(!empty($_GET['code']) && isset($_GET['code']))
{
    $code=$_GET['code'];
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING  => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT   => 30,

        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => array(
            "grant_type"    => "authorization_code",
            "client_secret" => "xxxxxxxx",
            "code"          => $_GET['code'],
            "client_id"     => " xxxxxxx",
            "redirect_uri"  => "https://localhost/test2.php"
        ),
        CURLOPT_HTTPHEADER => array(
            "api-version: alpha",
            "cache-control: no-cache",
            "content-type: application/json",
            "postman-token: 471a0741-8466-2e3f-0006-8b9c3794ef9d"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
}

它给了我以下回应:

  

400错误无效请求参数。

任何解决方案?

2 个答案:

答案 0 :(得分:0)

我认为你已经使用postman生成了这个php代码段,你可以参考我的代码

public function fobOath($code)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "grant_type=authorization_code&client_secret=<your_client_secret>&code=<your_code>&client_id=<your_client_id>&redirect_uri=<your_callback_address>",
        CURLOPT_HTTPHEADER => array(
            "api-version: alpha",
            "cache-control: no-cache",
            "content-type: application/x-www-form-urlencoded"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }

}

答案 1 :(得分:0)

我有同样的问题,我在转义JSON字符串后解决了它。

  

试试这个

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.freshbooks.com/auth/oauth/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "{\n    \"grant_type\": \"authorization_code\",\n    \"client_secret\": \"Your client secret\",\n    \"code\": \"your code\",\n    \"client_id\": \"your client id\",\n    \"redirect_uri\": \"https://www.yoururl.com/redirect\"\n}",
    CURLOPT_HTTPHEADER => array(
        "api-version: alpha",
        "cache-control: no-cache",
        "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}