2017-05-04 1 views
0

J'essaie d'obtenir mon "jeton d'accès de longue durée" en utilisant CURL/PHP mais je reçois l'erreur "Paramètres manquants pour client_id, client_secret, code, grant_type, redirect_uri" .Paramètres manquants lors de la requête OAUTH token survey monkey v3

L'URL que j'appelle est celle où vous pouvez voir clairement les paramètres que j'essaie de transmettre!

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 = code_autorisation

J'utilise également le type de contenu "application/x-www-form-urlencoded" selon les docs (voir ci-dessous).

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

Où vais-je tort?

Répondre

2

Droit de la documentation, il ressemble à obtenir le jeton de longue durée dont vous avez besoin pour poster vos champs:

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

Lorsque vous ajoutez vos paramètres sur votre URL envoyez alors comme paramètres de requête GET

Vous devez mettre votre chaîne de données dans CURL POSTFIELDS et ne pas coder json

Le PHP réponse

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

Merci - votre réponse est logique. Je vais essayer dans la matinée et rapporter/accepter votre réponse si cela fonctionne :) –

+0

le code ci-dessus a fonctionné parfaitement !! Merci beaucoup pour votre aide :) –