perl - 我的 ETrade OAuth 获取 token 请求有什么问题?

标签 perl command-line oauth wget etrade-api

服务器正在响应一条不太有用的消息。

Unable to get a request token: Request for https://etwssandbox.etrade.com/oauth/sandbox/request_token?oauth_callback=oob&oauth_consumer_key=aaf0812a4bcc6e4c21783af47cf88237&oauth_nonce=3495463522&oauth_signature=ykqRaZc18GwIoqHtYqtxzsMq4xs%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1371092839&oauth_version=1.0 failed,HTTP/1.1 400 Bad Request

Connection: close
Content-Length: 62
Client-Date: Thu, 13 Jun 2013 03:07:19 GMT
Client-Peer: 12.153.224.230:443
Client-Response-Num: 1
Client-SSL-Cert-Issuer: /C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 Secure Server CA - G3
Client-SSL-Cert-Subject: /C=US/ST=New York/L=New York/O=ETRADE FINANCIAL CORPORATION/OU=Global Information Security/CN=etwssandbox.etrade.com
Client-SSL-Cipher: RC4-MD5

<html><body><b>Http/1.1 400 Bad Request</b></body> </html>

好的,我会尝试使用标题。所有必需的参数都存在。

$ wget -d -O- --header='Authorization: OAuth realm="",oauth_callback="oob",oauth_consumer_key="aaf0812a4bcc6e4c21783af47cf88237",oauth_nonce="3495463522",oauth_signature="ykqRaZc18GwIoqHtYqtxzsMq4xs%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1371092839",oauth_version="1.0"' 'https://etwssandbox.etrade.com/oauth/sandbox/request_token'

Setting --output-document (outputdocument) to -
Setting --header (header) to Authorization: OAuth realm="",oauth_callback="oob",oauth_consumer_key="aaf0812a4bcc6e4c21783af47cf88237",oauth_nonce="3495463522",oauth_signature="ykqRaZc18GwIoqHtYqtxzsMq4xs%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1371092839"
DEBUG output created by Wget 1.13.4 on cygwin.

URI encoding = `UTF-8'
--2013-06-12 23:08:33--  https://etwssandbox.etrade.com/oauth/sandbox/request_token
Resolving etwssandbox.etrade.com (etwssandbox.etrade.com)... 12.153.224.230, 198.93.34.230
Caching etwssandbox.etrade.com => 12.153.224.230 198.93.34.230
Connecting to etwssandbox.etrade.com (etwssandbox.etrade.com)|12.153.224.230|:443... connected.
Created socket 3.
Releasing 0x80733128 (new refcount 1).

---request begin---
GET /oauth/sandbox/request_token HTTP/1.1
User-Agent: Wget/1.13.4 (cygwin)
Accept: */*
Host: etwssandbox.etrade.com
Connection: Keep-Alive
Authorization: OAuth realm="",oauth_callback="oob",oauth_consumer_key="aaf0812a4bcc6e4c21783af47cf88237",oauth_nonce="3495463522",oauth_signature="ykqRaZc18GwIoqHtYqtxzsMq4xs%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1371092839"

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 400 Bad Request
Content-Length:62
Connection: close

---response end---
400 Bad Request
2013-06-12 23:08:34 ERROR 400: Bad Request.

那仍然不起作用。让我验证一下签名。请注意,我的 key 和 secret 是正确的。

第一个 URL 对所有参数进行编码,形成用于签名的基本字符串。

$ perl -MURI::Escape -e "print uri_escape('oauth_callback=oob&oauth_consumer_key=aaf0812a4bcc6e4c21783af47cf88237&oauth_nonce=3495463522&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1371092839&oauth_version=1.0')"

oauth_callback%3Doob%26oauth_consumer_key%3Daaf0812a4bcc6e4c21783af47cf88237%26oauth_nonce%3D3495463522%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1371092839%26oauth_version%3D1.0

现在使用 HMAC-SHA1 进行哈希处理,使用 Base64 进行编码(末尾没有换行符),并对生成的签名进行 URL 编码。

消费者 key 末尾有一个 & 符号,因为我们还没有 token key (它是空的)。

$ perl -MDigest::HMAC_SHA1=hmac_sha1 -MMIME::Base64 -MURI::Escape -e "print uri_escape(encode_base64(hmac_sha1('GET&https%3A%2F%2Fetwssandbox.etrade.com%2Foauth%2Fsandbox%2Frequest_token&oauth_callback%3Doob%26oauth_consumer_key%3Daaf0812a4bcc6e4c21783af47cf88237%26oauth_nonce%3D3495463522%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1371092839%26oauth_version%3D1.0', 'xxxxxxxxxxxxxxxxxxxx&'), ''))"

ykqRaZc18GwIoqHtYqtxzsMq4xs%3D

此签名与上面的签名相符。

规范在这里:http://oauth.net/core/1.0a/#signing_process

ETrade 规范位于:https://us.etrade.com/ctnt/dev-portal/getDetail?contentUri=V0_Documentation-AuthorizationAPI-GetRequestToken

最佳答案

ETrade 的文档已损坏。他们指定在Sandbox环境中使用不同的主机和URL

https://us.etrade.com/ctnt/dev-portal/getContent?contentUri=V0_Documentation-DeveloperGuides-Sandbox

但对于 OAuth 来说则不然。这部分从未被提及,我必须查看他们的 SDK 之一的源代码才能找到答案。

|Environment| URL |
|Production |https://etws.etrade.com/{module}/rest/{API}  |
|Sandbox    |https://etwssandbox.etrade.com/{module}/sandbox/rest/{API} |

关于perl - 我的 ETrade OAuth 获取 token 请求有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079250/

相关文章:

windows - 文件类型 .pl 关联并使用 cmd.exe 运行脚本

perl - 查找列中的唯一值并将唯一值替换为数字

windows - find.exe 有问题?

r - R 中使用的 Github API 的授权码

perl - 使用 Perl 访问 50 万页

json - 如何在 Perl 中为 JSON 数组分配名称

macos - 如何以编程方式为未识别的 .app 添加异常

windows - 如何使用批处理文件使结果在完成时保持可见

api - OAuth v2 (Google API) 过期访问 token

web-services - 我可以使用 Facebook OAuth 来保护我的 RESTful Web 服务吗?