2017-02-17 137 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