2

请你帮帮我。我正在尝试为 7 Digital 的高级 API 生成 HMAC-SHA1 哈希,但是我无法获得正确的签名。我关注了这篇文章https://oauth1.wp-api.org/docs/basics/Signing.html并得到了以下代码:

 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;

    }

请注意,我还尝试使用 base64_encode 对签名进行编码不起作用,并且还尝试像这样对密钥进行 urlencode:

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

我也使用过那里的参考页面。

以上输出:

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

非常感谢您的帮助,在此先感谢您!!

4

1 回答 1

2

所以,在拔了很多头发后,我遇到了以下问题:

  1. 需要像这样对 sha1 哈希进行 urlencoded:

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

  2. $baseString 在 URL 的末尾有一个 &。
  3. 关键是你的 oauth_consumer_secret 后跟一个 &

这是其他有此问题的人的完整内容!

    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)));
    }
于 2016-11-01T11:51:50.670 回答