How to get oauth signature, nonce and timestamp with php? -
i trying stablish connection api uses oauth 1.0 authorization.
i have following class create nonce, , create timestamp.
<?php /** * * tiny nonce generator variable time-outs. * * no database required. * each nonce has own salt. * */ class nonceutil { /** * generate nonce. * * generated string contains 3 parts, seperated comma. * first part individual salt. seconds part * time until nonce valid. third part hash of * salt, time, , secret value. * * @param $secret required string @ least 10 characters. * same value must passed check(). * * @param $timeoutseconds time in seconds until nonce * becomes invalid. * * @return string generated nonce. * */ public static function generate($secret, $timeoutseconds=180) { if (is_string($secret) == false || strlen($secret) < 10) { throw new invalidargumentexception("missing valid secret"); } $salt = self::generatesalt(); $time = time(); $maxtime = $time + $timeoutseconds; $nonce = $salt . "," . $maxtime . "," . sha1( $salt . $secret . $maxtime ); return $nonce; } /** * check generated nonce. * * @param $secret secret string passed generate(). * * @returns bool whether nonce valid. */ public static function check($secret, $nonce) { if (is_string($nonce) == false) { return false; } $a = explode(',', $nonce); if (count($a) != 3) { return false; } $salt = $a[0]; $maxtime = intval($a[1]); $hash = $a[2]; $back = sha1( $salt . $secret . $maxtime ); if ($back != $hash) { return false; } if (time() > $maxtime) { return false; } return true; } private static function generatesalt() { $length = 6; $chars='1234567890qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm'; $ll = strlen($chars)-1; $o = ''; while (strlen($o) < $length) { $o .= $chars[ rand(0, $ll) ]; } return $o; } } ?>
the implementation is:
define('nonce_secret', 'jvtgophiq108pqw9hej'); require_once('nonceutil.php'); $nonce = nonceutil::generate(nonce_secret, 1); $r = nonceutil::check(nonce_secret, $nonce); $noncegenetared=explode(",",$nonce); $noncevalue=$noncegenetared[0]; $noncetimestamp=$noncegenetared[1]; $noncehash=$noncegenetared[2]; //not useful connection, nonce library validation. print_r($noncevalue); print_r($noncetimestamp);
first problem find requests nonce , timestamp value seems not legit.
on other hand, ensure if problem that, try request postman , works, update requests, copy nonce , timestamp , execute request. fails error oauth_signature...
if copy oauth signature of postman works, , expected response.
my problems about, how generate nonce , timestamp on php oauth api request? , how can cast consumer secret oauth signature?
Comments
Post a Comment