3

I try to authenticate with LinkedIn API with OAuth2. Code:

if ((isset($_GET["code"])) AND (isset($_GET["state"]))) {
        $code = $_GET["code"];
        $state = $_GET["state"];

        $curl_request = curl_init();
        curl_setopt_array($curl_request, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => "https://www.linkedin.com/uas/oauth2/accessToken",
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => array(
                grant_type => "authorization_code",
                code => "$code",
                redirect_uri => "SECRET",
                client_id => "SECRET",
                client_secret => "SECRET"
            )
        ));

        $curl_result = curl_exec($curl_request);

        var_dump($curl_result);

}

I got this message:

string(153) "{"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : client_id","error":"invalid_request"}"

Could you help m?

Hans Z.
  • 41,402
  • 9
  • 80
  • 105
Paulius Vitkus
  • 129
  • 1
  • 4
  • 12

3 Answers3

4

You need to add the following header in your cURL POST call to ensure that the values you are passing in the body of your POST are interpreted correctly:

Content-Type: application/x-www-form-urlencoded

For additional info, see http://tools.ietf.org/html/rfc6749#section-4.1.3 for the actual OAuth 2.0 RFC spec.

Justin Kominar
  • 3,246
  • 1
  • 12
  • 14
  • 1
    +1, http://php.net/manual/en/function.curl-setopt.php says: "If value is an array, the Content-Type header will be set to multipart/form-data. " – Hans Z. Feb 18 '15 at 18:27
  • Justin, Hans Z., I just added `CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),` above `CURLOPT_RETURNTRANSFER` - is it enough? Because i get the same error message. Can't understand clearly all of this. Could you get me some extra URL, where I can find examples or something? – Paulius Vitkus Feb 18 '15 at 19:38
  • 1
    Maybe this will help? http://stackoverflow.com/questions/18913345/curl-posting-with-header-application-x-www-form-urlencoded – Justin Kominar Feb 19 '15 at 18:40
3

Use: http_build_query

curl_setopt_array($curl, array(
            CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => 'https://www.linkedin.com/uas/oauth2/accessToken',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => http_build_query (array(
                client_id => "secret",
                client_secret => "secret",
                grant_type => "authorization_code",
                redirect_uri => "url",
                code => $code
            )),
        ));
Pablo
  • 119
  • 2
  • 4
0

I am weirded out by this, but I just had the same problem and when I just moved client id and secret to beginning of the POST array and everything worked. I don't even.

ax23w4
  • 31
  • 1
  • 5