【问题标题】:Missing parameters when requesting OAUTH token survey monkey v3请求 OAUTH 令牌调查猴子 v3 时缺少参数
【发布时间】:2017-10-03 06:36:09
【问题描述】:

我正在尝试使用 CURL/PHP 获取我的“长期访问令牌”,但收到错误消息“缺少 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=授权码

我还根据文档使用“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;
}

我哪里错了?

【问题讨论】:

    标签: php oauth-2.0 surveymonkey


    【解决方案1】:

    直接从文档中获取发布字段所需的长期令牌:

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

    【讨论】:

      猜你喜欢
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多