如何在实验api中生成oath_signature

问题描述 投票:1回答:1

我想在php中生成oath_signature但是实验文档是用.net编写的。我试着询问表格实验开发人员,但他反应不好。我使用以下代码来开发oath_signature:

$ timestamp = 1461944620; //时间戳

$ nonce = 3774707; // nuncio

$ consumerKey =“e172b7b1-056f-48be-aeca-a641a60c88ea”; //消费者密钥 - 由experian dev提供。

$ consumerSecret =“1c339256-2e1c-48bb-82c2-8e33abe550fd”; //消费者秘密 - 由experian dev提供。

$ url =“https://demo.backgroundchecking.com/API/Service/ProductCodes”; //网址 - 由experian dev提供。

$ httpMethod =“GET”;

$ params =“oauth_consumer_key =”。$ consumerKey。“&oauth_nonce =”。$ nonce。“&oauth_signature_method = HMAC-SHA1&oauth_timestamp =”。$ timestamp。“&oauth_version = 1.0”;

$ urlEncoded = urlencode($ url); //网址编码

$ paramsEncoded = urlencode($ params); //网址编码

$ signature Base = $ httpMethod。“&”。$ urlEncoded。“&”。$ param Encoded; //签名库 - 根据您的文档生成

$ key = urlencode($ consumerSecret)。“&”; // mysecretkey& - url编码秘密凯,然后是&符号

$ signatureBytes = hash_hmac(“sha1”,$ signatureBase,$ key); //转换为sha1

$ signature = base64_encode($ signature Bytes); // base64加密

$ signatureEncoded = urlencode($ signature); //签名网址编码。这是最终签名,与我在之前的电子邮件中描述您的签名不符。

我已经去了“NjdhNTUyMDU2Y2M2MDcyMTQ2ZDNkZWFjYjUxODVlOGQ3MGZlZWM1Mg%3D%3D”签名,但是开发人员说,使用上面的细节它必须生成“Z6VSBWzGByFG096stRhejXD%2b7FI%3d”誓言签名。

以下是生成oath_signature的文档:

手动生成签名1.我们需要按字母顺序对查询参数进行排序,并添加一些参数,然后将它们组合成一个字符串。此字符串看起来像标准查询字符串,但您实际上并不打算将其发送到服务器。 oauth_consumer_key = myconsumerkey&oauth_nonce = 123456&oauth_signature_method = HMAC-SHA1&oauth_timestamp = 999999&oauth_version = 1.0重要的是,字符串看起来与此完全相同,只有斜体字的三个值被您的值替换。

  1. 我们需要生成一个可以进行哈希处理的签名基本字符串。此字符串由HTTP方法,URL编码的URL和URL编码的查询字符串组成。

假设您正在发布“以上提供网址(demo.backgroundchecking.com/API/Service/ProductCodes)”,那么签名库将如下所示(换行符只是为了可读性,这应该都是一行):3。POST& 4. https%3A%2F%2Fuat.backgroundchecking.com%2FAPI%2FData%2FRequestData&5。oauth_consumer_key%3Dmyconsumerkey%26oauth_nonce%3D123456%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D999999%26oauth_version%3D1.0

如您所见,Signature Base字符串由三个单独的元素组成,HTTP方法,URL和步骤1中的参数,所有元素都以&分隔。在将这三个部分中的每个部分组合到签名库字符串之前对它们进行URL编码是很重要的,否则分离&也将被编码,您的请求将失败。还要注意,十六进制值必须为大写,即%2F而不是%2f。请注意此示例中的uat.backgroundchecking.com当然需要更改为您正在使用的系统的相应URL。

再举一个例子,如果你是从demo.backgroundchecking.com/API/Service/ProductCodes获取,那么Signature Base字符串看起来就像这样(方法和URL不同):GET&https%3A%2F%2Fuat.backgroundchecking .com%2FAPI%2FService%2FProductCodes&oauth_consumer_key%3Dmyconsumerkey%26oauth_nonce%3D123456%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D999999%26oauth_version%3D1.0

  1. 接下来我们需要获取生成哈希的密钥。关键是消费者秘密后跟一个&符,所以“mysecretkey&”就是这样。请注意,如果使用者密钥包含URL中不合法的字符,则需要在添加&符号之前对使用者密钥进行URL编码。
  2. 然后,我们需要生成HMAC-SHA1哈希并将其作为Base64编码的字符串返回。这通常通过库完成。一般来说,您应该期望使用ASCII编码将Key和Signature Base转换为字节数组,然后使用步骤3中的Key生成Signature Base的哈希值。
  3. 由于Base64包含某些在URL中不合法的字符,因此我们需要对URL进行编码,然后才能使用它。

示例是:1。[int] $ timestamp =((Get-Date).ToUniversalTime() - (Get-Date -Date“1970-01-01 00:00:00Z”)。ToUniversalTime())。TotalSeconds

  1. $ timestamp = 999999 #override带有我们的样本值
  2. $ nonce = 123456 #in production,确保每个请求都是随机的
  3. $ consumerKey =“myconsumerkey”
  4. $ consumerSecret =“mysecretkey”
  5. $ url =与上面提供的相同
  6. $ httpMethod =“POST”
  7. $ regex = [regex]'%[a-f0-9] {2}'#oAuth要求十六进制值为大写,.NET默认为小写,所以我们需要用正则表达式做一些魔术
  8. $ params =“oauth_consumer_key =”+ $ consumerKey +“&oauth_nonce =”+ $ nonce +“&oauth_signature_method = HMAC-SHA1&oauth_timestamp =”+ $ timestamp +“&oauth_version = 1.0”
  9. $ urlEncoded = $ regex.Replace([System.Web.HttpUtility] :: UrlEncode($ url),{param($ m)$ m.Value.ToUpperInvariant()})
  10. $ paramsEncoded = $ regex.Replace([System.Web.HttpUtility] :: UrlEncode($ params),{param($ m)$ m.Value.ToUpperInvariant()})
  11. $ signatureBase = $ httpMethod +“&”+ $ urlEncoded +“&”+ $ paramsEncoded
  12. $ key = $ regex.Replace([System.Web.HttpUtility] :: UrlEncode($ consumerSecret),{param($ m)$ m.Value.ToUpperInvariant()})+“&”
  13. $ hmacsha = New-Object System.Security.Cryptography.HMACSHA1
  14. $ hmacsha.key = [Text.Encoding] :: ASCII.GetBytes($ key)
  15. $ signatureBytes = $ hmacsha.ComputeHash([Text.Encoding] :: ASCII.GetBytes($ signatureBase))
  16. $ signature = [System.Convert] :: ToBase64String($ signatureBytes)
  17. $ signatureEncoded = $ regex.Replace([System.Web.HttpUtility] :: UrlEncode($ signature),{param($ m)$ m.Value.ToUpperInvariant()})
php .net
1个回答
0
投票
$timestamp = time(); 
$nonce = 5678765; 
$consumerKey = "consumerkey"; 
$consumerSecret = "secret"; 
$url = "demo.backgroundchecking.com/API/Service/ProductCodes";
$httpMethod = "GET"; 
$params = "oauth_consumer_key=".$consumerKey."&oauth_nonce=".$nonce."&oauth_signature_meth‌​od=HMAC-SHA1&oauth_timestamp=".$timestamp."&oauth_version=1.0"; 
$urlEncoded = urlencode($url); 
$paramsEncoded = urlencode($params); 
$signatureBase = $httpMethod."&".$urlEncoded."&".$paramsEncoded; 
$key = urlencode($consumerSecret)."&"; 
$signature = urlencode(base64_encode(hash_hmac('sha1', $signatureBase, $key, true)));
© www.soinside.com 2019 - 2024. All rights reserved.