0
votes

I'm trying to obtain my "long lived access token" using CURL/PHP but I'm receiving the error "Missing parameters for client_id, client_secret, code, grant_type, redirect_uri".

The URL I'm calling is where you can clearly see the parameters I'm trying to pass in!

https://api.surveymonkey.net/oauth/token?client_secret='.urlencode($client_secret).'&code='.urlencode($short_token).'&redirect_uri='.urlencode($redirect_url).'&client_id='.urlencode($client_id).'&grant_type=authorization_code

I'm also using the content-type of "application/x-www-form-urlencoded" as per the docs (see below).

My CURL request:

function survey_monkey_curl_request($url, $params=[], $request_type = 'get', $access_token) {

  print_r($url);

  $ch = curl_init();

  $headers = [
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: bearer " .$access_token
  ];
  $opts = [
    CURLOPT_URL => $url,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => 0,
  ];
  if ($request_type == 'post') {
    $opts[CURLOPT_POST] = 1;
    //$opts[CURLOPT_POSTFIELDS] = json_encode($params);
  }
  if ($request_type == 'patch') {
    $opts[CURLOPT_CUSTOMREQUEST] = "PATCH";
    $opts[CURLOPT_POSTFIELDS] = json_encode($params);
  }
  curl_setopt_array($ch, $opts);
  $result = curl_exec($ch);

  if ($result === false)  {
    curl_close($ch);
    throw new Exception(curl_error($ch));
  }
  curl_close($ch);
  return $result;
}

Where am I going wrong?

1

1 Answers

2
votes

Straight from the documentation it looks like to get the long-lived token you need to post your fields:

//Exchange for long-lived token

curl -i -X POST https://api.surveymonkey.net/oauth/token -d \
"client_secret=YOUR_CLIENT_SECRET \
&code=AUTH_CODE \
&redirect_uri=YOUR_REDIRECT_URI \
&client_id=YOUR_CLIENT_ID \
&grant_type=authorization_code"

https://developer.surveymonkey.com/api/v3/?shell#new-authentication

When you append your parameters onto your url you are sending then as GET request paramters

You need to put your data string into CURL POSTFIELDS and do not json encode

The PHP Answer

<?php

$ch = curl_init();

$data = [
    'client_secret' => $YOUR_CLIENT_SECRET,
    'code' => $AUTH_CODE,
    'redirect_url' => $YOUR_REDIRECT_URI,
    'client_id' => $YOUR_CLIENT_ID,
    'grant_type' => 'authorization_code'
];//set your data as an array

$headers = [
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: bearer " . $access_token
];
$opts = [
    CURLOPT_URL => $url,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
    $opts[CURLOPT_POST] = 1;
    $opts[CURLOPT_POSTFIELDS] = http_build_query($data);// this will build your data string from the array
}

curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
curl_close($ch);
return $result;