2017-02-17 140 views
1

我試圖使用CURL向linkedin API發送發佈請求。出於某種原因,我收到一條迴應,說我缺少一個變量。正確獲取CURL發佈參數(PHP)

array:2 [▼ 
    "error" => "missing_parameter" 
    "error_description" => "A required parameter "client_id" is missing" 
] 

這是我的代碼,我可以向你保證設置了cliend_id。

$code = $request->get("code"); 
    $state = $request->get("state"); 
    $redirect_uri = "http://example.com/linkedin/callback"; 
    $client_id = "1242435657"; 
    $client_secret = "XXXXXXXXXX"; 

    $url = "https://www.linkedin.com/oauth/v2/accessToken"; 

    $params = array(
     "grant_type" => 'authorization_code', 
     "code" => $code, 
     "redirect_uri" => $redirect_uri, 
     "client_id" => $client_id, 
     "client_secret" => $client_key, 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, true); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
    $output = curl_exec($ch); 
    $info = curl_getinfo($ch); 
    curl_close($ch); 

    // create an array from the data that is sent back from the API 
    $result = json_decode($output, 1); 

有無論如何,我可以調試此發佈請求嗎?

+0

嘗試改變'curl_setopt($ CH,CURLOPT_POST,真);'到'curl_setopt($ CH,CURLOPT_POST,計數($ params)方法);' –

+0

@BojanRadaković這不會改變任何東西 –

回答

0

刪除最後一個屬性末尾的逗號,然後再試一次?

$params = array(
     "grant_type" => 'authorization_code', 
     "code" => $code, 
     "redirect_uri" => $redirect_uri, 
     "client_id" => $client_id, 
     "client_secret" => $client_key, <========= 
    ); 
+0

這不會改變任何東西。 –

+0

也許你應該將它們作爲GET請求發送。我試過這個url:_https://www.linkedin.com/oauth/v2/accessToken/?client_id = 123&client_secret = 123&grant_type = authorization_code&redirect_uri = uri&code = code_這是我收到的錯誤:_ {「error」:「 invalid_client_id「,」error_description「:」傳入的client_id無效\「123 \」「} _。至少它不會說:_ {「error」:「missing_parameter」,「error_description」:「缺少一個必需的參數\」client_id \「」} _現在。 – Aj334