Katie
Katie

Reputation: 341

7 Digital API invalid signature

Please can you help me. I am trying to generate a HMAC-SHA1 hash for 7 Digital's Premium API however I cannot get the correct signiture. I have followed this post https://oauth1.wp-api.org/docs/basics/Signing.html and have got the following code :

 public function __construct(){
        $this->timestamp = time();
        $this->_nonce = rand();
        $this->consumerKey = 'KEY';
        $this->consumerSecret = 'SECRET';
        $this->signitureMethod = 'HMAC-SHA1';


    }

    public function buildSignature()
    {          
        $method = 'GET&';
        $url = 'https://api.7digital.com/1.2/oauth/requesttoken';
        $params = 'oauth_consumer_key='.$this->consumerKey.'&oauth_nonce='.$this->_nonce.'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.$this->timestamp.'&oauth_version=1.0';
        $baseString = $method.urlencode($url).'&'.urlencode($params);
        $key = $this->consumerKey . '&' . $this->consumerSecret;

        $signature = hash_hmac( 'sha1', $baseString,  $key );
        $string = 'https://api.7digital.com/1.2/oauth/requesttoken?oauth_consumer_key='.$this->consumerKey.'&oauth_nonce='.$this->_nonce.'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.$this->timestamp.'&oauth_version=1.0&oauth_signature='.$signature;
        echo $string;

    }

Please note I have also tried to encode the signature with base64_encode doesn't work and also have tried to urlencode the key like so :

            $key = urlencode($this->consumerKey) . '&' . urlencode($this->consumerSecret);

I have used there reference page as well.

The above outputs :

https://api.7digital.com/1.2/oauth/requesttoken?oauth_consumer_key=KEY&oauth_nonce=495776478&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1477934267&oauth_version=1.0&oauth_signature=fe3a5c61618d2a144b608570ce2be36f4c3b3e55

Your help would be much appreciated thank you in advance!!

Upvotes: 2

Views: 228

Answers (1)

Katie
Katie

Reputation: 341

So, after alot of pulling hair out I have the following problems :

  1. The sha1 hash needed to be urlencoded like so :

    urlencode(base64_encode(hash_hmac( 'sha1' , $baseString, $key , true)));

  2. The $baseString had an & at the end of the url.
  3. The key is just your oauth_consumer_secret followed by an &

Here is this in full for anyone else with this problem!

    public function buildUrl() {
        $params = [
           'method' => $this->method,
           'url' => urlencode($this->url),
           'additionalParams' =>  urlencode($this->additionalParams . '&'),
           'params' => urlencode($this->params),
           ];
        if ($this->additionalParams) {
            $baseString = $params['method'] . '&' . $params['url'] . '&' . $params['additionalParams'] . $params['params'];
        }
        else{
            $baseString = $params['method'] . '&' . $params['url'] . '&' .  $params['params'];
        }
        $this->_signiture =  $this->getSigniture($baseString, $this->key);

        if ($params['additionalParams']) {
            $url = urldecode($params['url']) . '?' . urldecode($params['additionalParams']) . urldecode($params['params']) . '&oauth_signature='.$this->_signiture;
        }
        else {
            $url = urldecode($params['url']) . '?' . urldecode($params['params']) . '&'. urldecode($params['additionalParams']) . '&oauth_signature='.$this->_signiture;
        }
     echo $url;
    }
  function getSigniture($baseString, $key){
        return   urlencode(base64_encode(hash_hmac( 'sha1' , $baseString,  $key , true)));
    }

Upvotes: 2

Related Questions