2017-09-17 1 views
0

Je reçois une erreur invalide de signature OAUTH. Quelqu'un peut-il s'il vous plaît vérifier ce que je fais mal. Ci-dessous est mon code PHP, j'ai couru à travers beaucoup de docs et de site Web auth bible aussi mais ci-dessous le code ne fonctionne pas! aidez svp! J'ai enlevé ma clé et mon secret pour des raisons de sécurité.oauth signature Invalide - Smugmug - PHP - cURL

============================================== ===============

$host = "https://secure.smugmug.com/services/oauth/1.0a/getRequestToken?"; 
    $path   = ""; 
    $request_method = "GET"; 
    $consumer_key = "mykey"; 
    $consumer_secret = "mysecret"; 


    //collect the data (as an associative array) 
    $oauth_data = array(
     'oauth_consumer_key'  => $consumer_key, 
     'oauth_nonce'   => (string)mt_rand(), // a stronger nonce is recommended 
     'oauth_signature_method' => 'HMAC-SHA1', 
     'oauth_timestamp'  => time(), 
     'oauth_callback'  => "http://mywebsite/callback", 
     'oauth_version'   => '1.0' 
    ); 


    $arr = array(); 

    //normalize the parameters - apply url encoding separately to each key/value pair 
    foreach($oauth_data AS $key => $value) { 
     $encoded_key = rawurlencode($key); 
     $encoded_val = rawurlencode($value); 
     $arr[$encoded_key] = $encoded_val; 
    } 

    //normalize the parameters - sort the encoded data by key 
    ksort($arr); 

    //normalize the parameters - list the data members as string in <key>=<value> format and insert "&" between each pair 
    // http_build_query automatically encodes, but our parameters are already encoded, 
    // so we urldecode to prevent double-encoding 
    $querystring = urldecode(http_build_query($arr, '', '&')); 

    //normalize the parameters - apply urlencoding to the resulting string 
    $encoded_query_string = rawurlencode($querystring); 


$url = $host; 

    // mash everything together for the text to hash 
    $base_string = strtoupper($request_method). "&". rawurlencode($url).  "&". $encoded_query_string; 


    $key = rawurlencode($consumer_secret)."&"; 

    // generate the hash 
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); 


    $url = $host . "&" . $querystring . "&oauth_signature=" . $signature; 


    // Initiate curl 
    $ch = curl_init(); 

// Disable SSL verification 


    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
// Will return the response, if false it print the response 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Set the url 
curl_setopt($ch, CURLOPT_URL,$url); 
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

    curl_setopt($ch, CURLOPT_HEADER, 1); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); 

$result=curl_exec($ch); 

// Will dump a beauty json :3 

print_r($result); 

curl_close($ch); 

Répondre

1

Je ne sais pas si vous êtes toujours face à ce problème, mais vous pouvez essayer de ne pas coder la signature de base64.

Modifier ceci:

// generate the hash 
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); 

à ceci:

// generate the hash 
    $signature = base64_encode(hash_hmac('sha1', $base_string, $key, true)); 

En outre, vous devez spécifier la méthode de requête

// GET (should be your case, otherwise you are going to recieve a "Invalid Method" response) 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
// POST 
curl_setopt($ch, CURLOPT_POST, true); 

Assurez-vous qu'il correspond à la méthode utilisée pour générer la Signature.