如何为OAuth生成签名

时间:2016-07-19 21:18:08

标签: php rest oauth

我目前正在尝试生成签名,以便在线对API进行API调用,但是我不断收到身份验证错误。我确定签名部分是我出错的地方。这是不正确的:

$this->_headers = array(
        'Authorization: '.urlencode('OAuth oauth_token="'.$this->_auth_token.'",oauth_nonce="ea9ec8429b68d6b77cd5600adbbb0456",oauth_consumer_key="'.$this->_consumer_key.'",oauth_signature_method="HMAC-SHA1", oauth_timestamp="'.time().'", oauth_version ="1.0"oauth_signature="'.$this->_signature.'"').''

    );

现在,当我发送请求时,这里是标题:

$this->_auth_token

我收到401授权回复。我签名不正确吗?

编辑:此处未包含的所有字段(即A [1, 2, 3, 4] B [5, 6, 1, 9] C [3, 7, 5, 8] Results A [1, 2, 3, 4] B [5, 6, 9] ->original order is preserved (i.e. 5 is before 6 which is before 9) C [7, 8] )都已设置。

3 个答案:

答案 0 :(得分:2)

对于任何可能将此作为自己集成基础的人,最初发布的代码还有另一个问题:

$key = rawurlencode($this->_consumer_secret.'&'.$this->_token_secret);

应该是

$key = rawurlencode($this->_consumer_secret).'&'.rawurlencode($this->_token_secret);

答案 1 :(得分:0)

此问题出现在基本字符串中:

.rawurlencode("oauth_consumer_key=".rawurlencode($this->_consumer_key).'&'
    .'&oauth_nonce='.rawurlencode('34604g54654y456546')

消费者密钥之后的&和oauth_nonce之前的 def gender(): var2 = input ("What is your gender? ") v1 = int (var4) v2 = int (var5) guyheight = v1+v2+13 girlhiehgt = v1+v2-13 v4 = guyheight/2 v5 = guyheight/2 if var2 == 'boy' or var2 == 'Boy' or var2 == 'guy' or var2 == 'Guy' or var2 == 'male' or var2 == 'Male' or var2 == 'man' or var2 == 'Man': print ("Your estimated full-fledged height is, {}.".format(v4)) elif var2 == 'girl' or var2 == 'Girl' or var2 == 'female' or var2 == 'Female' or var2 == 'woman' or var2 == 'Woman': print ("Your estimated full-fledged height is, {}.".format(v5)) else: print ("Sorry, your gender is invalid. Please re-enter the answer.") gender()

答案 2 :(得分:0)

在签名创建中,我认为它缺少对rawurlencode()的调用:

$this->_signature = rawurlencode(base64_encode(hash_hmac("sha1", $base, $key, true)));

相反:

$this->_signature = base64_encode(hash_hmac("sha1", $base, $key, true));