请求 OAUTH 令牌调查猴子 v3 时缺少参数 [英] Missing parameters when requesting OAUTH token survey monkey v3

查看:71
本文介绍了请求 OAUTH 令牌调查猴子 v3 时缺少参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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=authorization_code

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

我的 CURL 请求:

functionsurvey_monkey_curl_request($url, $params=[], $request_type = 'get', $access_token) {打印_r($url);$ch = curl_init();$headers = ["内容类型:应用程序/x-www-form-urlencoded",授权:持有人" .$access_token];$opts = [CURLOPT_URL =>$网址,CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_1_0,CURLOPT_HTTPHEADER =>$头,CURLOPT_FOLLOWLOCATION =>真的,CURLOPT_RETURNTRANSFER =>1、CURLOPT_SSL_VERIFYPEER =>0,];if ($request_type == 'post') {$opts[CURLOPT_POST] = 1;//$opts[CURLOPT_POSTFIELDS] = json_encode($params);}如果($request_type == '补丁'){$opts[CURLOPT_CUSTOMREQUEST] = "补丁";$opts[CURLOPT_POSTFIELDS] = json_encode($params);}curl_setopt_array($ch, $opts);$result = curl_exec($ch);如果($result === false){curl_close($ch);抛出新异常(curl_error($ch));}curl_close($ch);返回 $result;}

我哪里出错了?

解决方案

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

//交换长寿命令牌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 答案

$YOUR_CLIENT_SECRET,'代码' =>$AUTH_CODE,'redirect_url' =>$YOUR_REDIRECT_URI,'client_id' =>$YOUR_CLIENT_ID,'grant_type' =>'授权代码'];//将你的数据设置为数组$headers = ["内容类型:应用程序/x-www-form-urlencoded",授权:持有人".$access_token];$opts = [CURLOPT_URL =>$网址,CURLOPT_HTTP_VERSION =>CURL_HTTP_VERSION_1_0,CURLOPT_HTTPHEADER =>$头,CURLOPT_FOLLOWLOCATION =>真的,CURLOPT_RETURNTRANSFER =>1、CURLOPT_SSL_VERIFYPEER =>0,];if ($request_type == 'post') {$opts[CURLOPT_POST] = 1;$opts[CURLOPT_POSTFIELDS] = http_build_query($data);//这将从数组中构建您的数据字符串}curl_setopt_array($ch, $opts);$result = curl_exec($ch);curl_close($ch);返回 $result;

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?

解决方案

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;

这篇关于请求 OAUTH 令牌调查猴子 v3 时缺少参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆