请求 OAUTH 令牌调查猴子 v3 时缺少参数

Missing parameters when requesting OAUTH token survey monkey v3

我正在尝试使用 CURL/PHP 获取我的 "long lived access token",但我收到错误消息 "Missing parameters for client_id, client_secret, code, grant_type, redirect_uri"。

我调用的URL就是你可以清楚的看到我试图传入的参数!

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

我还根据文档(见下文)使用 "application/x-www-form-urlencoded" 的内容类型。

我的CURL请求:

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;
}

我哪里错了?

直接从文档中可以看出,您需要 post 您的字段获得长期有效的令牌:

//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

当您将参数附加到 url 时,您将作为 GET 请求参数发送

您需要将您的数据字符串放入 CURL POSTFIELDS 并且不json编码

PHP答案

<?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;