2014-11-01 68 views
0

我试图在SAS中使用curl从api下载数据。我有一个消费者密钥,消费者密钥,OAuth令牌,& OAuth令牌密钥。看起来我可以连接到API,但我收到一个错误,指出“HTTP传输:无法确定内容的长度”。有没有人有任何见解或想法?非常感谢。在sas中使用curl:api的oauth请求

代码:

dm 'clear log'; 

options; 

%let consumer_key =; 
%let consumer_secret=; 
%let oauth_token=; 
%let oauth_token_secret=; 

filename curl pipe "curl -X POST -k https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl --verbose --header 
'Authorization:OAuth oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0, 
oauth_signature_method=HMAC-SHA1,oauth_consumer_key={&consumer_key.},oauth_consumer_key_secret={&consumer_secret.}, 
oauth_token={&oauth_token.},oauth_token_secret={&oauth_token_secret.}"; 

data _null_; 
    infile curl lrecl=32767; 
    input; 
    put _infile_; 
run; 

登录:

dm 'clear log'; 

options; 
636 
637 %let consumer_key =; 
638 %let consumer_secret=; 
639 %let oauth_token=; 
640 %let oauth_token_secret=; 
641 
642  filename curl pipe "curl -X POST -k 
642! https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl --verbose --header 
643  'Authorization:OAuth 
643! oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0, 
WARNING: The quoted string currently being processed has become more than 262 characters long. 
      You might have unbalanced quotation marks. 
644  oauth_signature_method=HMAC-SHA1,oauth_consumer_key={&consumer_key.},oauth_consumer_ke 
644! y_secret={&consumer_secret.}, 
645  oauth_token={&oauth_token.},oauth_token_secret={&oauth_token_secret.}"; 
646 
647 data _null_; 
648 infile curl lrecl=32767; 
649 input; 
650 put _infile_; 
651 run; 

NOTE: The infile CURL is: 
     Unnamed Pipe Access Device, 

     PROCESS=curl -X POST -k https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=aapl 
     --verbose --header 'Authorization:OAuth 
     oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0, 

     oauth_signature_method=HMAC-SHA1,oauth_consumer_key={ 
     JXpV},oauth_consumer_key_secret={}, 
     oauth_token={},oauth_token_secret={}, 
     RECFM=V,LRECL=32767 

Fault Name: HttpRequestReceiveError 
Error Type: Default 
Description: Http request received failed 
Root Cause Code: -19013 
Root Cause : HTTP Transport: Couldn't determine the content length 
Binding State: CLIENT_CONNECTION_ESTABLISHED 
Service: null 
Endpoint: null 
Operation (Client): 

--_curl_--oauth_signature_method=HMAC-SHA1,oauth_consumer_key= 

--_curl_--oauth_token=,oauth_token_secret=Q 

Stderr output: 
* Adding handle: conn: 0x1ea8850 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 0 (0x1ea8850) send_pipe: 1, recv_pipe: 0 
    Total  Received Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0* About to 
connect() to api.tradeking.com port 443 (#0) 
* Trying 206.132.7.9... 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0* Connected to 
api.tradeking.com (206.132.7.9) port 443 (#0) 
* SSLv3, TLS handshake, Client hello (1): 
} [data not shown] 
* SSLv3, TLS handshake, Server hello (2): 
{ [data not shown] 
* SSLv3, TLS handshake, CERT (11): 
{ [data not shown] 
* SSLv3, TLS handshake, Server finished (14): 
{ [data not shown] 
* SSLv3, TLS handshake, Client key exchange (16): 
} [data not shown] 
* SSLv3, TLS change cipher, Client hello (1): 
} [data not shown] 
* SSLv3, TLS handshake, Finished (20): 
} [data not shown] 
* SSLv3, TLS change cipher, Client hello (1): 
{ [data not shown] 

    0  0 0  0 0  0  0  0 --:--:-- 0:00:01 --:--:--  0* SSLv3, TLS 
handshake, Finished (20): 
{ [data not shown] 
* SSL connection using RC4-MD5 
* Server certificate: 
* subject: OU=Domain Control Validated; OU=COMODO SSL Wildcard; CN=*.tradeking.com 
* start date: 2013-06-09 00:00:00 GMT 
* expire date: 2016-06-08 23:59:59 GMT 
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO SSL CA 
* SSL certificate verify result: self signed certificate in certificate chain (19), 
continuing anyway. 
> POST /v1/market/ext/quotes.xml?symbols=aapl HTTP/1.1 

> User-Agent: curl/7.33.0 

> Host: api.tradeking.com 

> Accept: */* 

> 'Authorization:OAuth 

> 

< HTTP/1.1 411 Length Required 

< Connection: close 

< Content-Length: 284 

< 

{ [data not shown] 

100 284 100 284 0  0 178  0 0:00:01 0:00:01 --:--:-- 178 
* Closing connection 0 
* SSLv3, TLS alert, Client hello (1): 
} [data not shown] 
* Rebuilt URL to: 
oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0,/ 
* Adding handle: conn: 0x1f00560 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 1 (0x1f00560) send_pipe: 1, recv_pipe: 0 
* Could not resolve host: 
oauth_nonce=,oauth_timestamp=1359019570,oauth_version=1.0, 
* Closing connection 1 
curl: (6) Could not resolve host: 
oauth_nonce=4572616e48616d6d65724c61686176,oauth_timestamp=1359019570,oauth_version=1.0, 

[1/3]: 
oauth_signature_method=HMAC-SHA1,oauth_consumer_key= 
--> <stdout> 
* Rebuilt URL to: 
oauth_signature_method=HMAC-SHA1,oauth_consumer_key= 
/
* Adding handle: conn: 0x1ef10e0 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 2 (0x1ef10e0) send_pipe: 1, recv_pipe: 0 
* Could not resolve host: 
oauth_signature_method=HMAC-SHA1,oauth_consumer_key= 

* Closing connection 2 
curl: (6) Could not resolve host: 
oauth_signature_method=HMAC-SHA1,oauth_consumer_key= 


[1/2]: 
oauth_token=,oauth_token_secret= 
--> <stdout> 
* Rebuilt URL to: 
oauth_token=,oauth_token_secret= 
/
* Adding handle: conn: 0x1ef56c0 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 3 (0x1ef56c0) send_pipe: 1, recv_pipe: 0 
* Could not resolve host: 
oauth_token=,oauth_token_secret= 
* Closing connection 3 
curl: (6) Could not resolve host: 
oauth_token=,oauth_token_secret= 

NOTE: 12 records were read from the infile CURL. 
     The minimum record length was 0. 
     The maximum record length was 174. 
NOTE: DATA statement used (Total process time): 
     real time   1.84 seconds 
     cpu time   0.23 seconds 
+0

如果您通过网络浏览器获取相同的数据,是否有任何发送的头文件您未在代码中发送,可能需要这些头文件才能使其工作? – user667489 2014-11-01 09:55:34

+0

感谢您的想法。你如何通过网络浏览器测试api?我安装了ActivePython-2.7.8.10-win64-x64和Twisted-14.0.2.win-amd64-py2.7,但没有任何工作。有更容易的方法吗? – user3806824 2014-11-02 06:38:41

+1

对不起,当我看到你的问题时,那部分没有正确扫描。尽管如此,一般的观点仍然适用 - 如果你有一个有效的API调用示例(即不使用cURL),请查看产生的流量,并查看是否有任何内容离开cURL调用。 – user667489 2014-11-02 08:55:02

回答

0

要做的第一件事就是尝试在命令行中运行你的卷曲的语句。如果您得到有效的答复,这是一个SAS问题,否则这是您的cURL请求的问题。

您是否尝试在您的cURL声明中添加-k选项,看看您是否发出https请求?

哦,还有,任何宏变量都包含包含&符号,百分号,双引号或单引号的值吗?这些都可能导致问题 - 特别是&符号,因为它们需要用插入符号^进一步掩盖。

+0

罗伯特,非常感谢您的想法。我相信这个问题与我的cURL声明中的oauth标题相关。 (注意,在我的api键中没有奇怪的符号。)此外,我尝试了使用和不使用标题的cmd行运行cURL(带-k)的建议,并得到相同的错误:“无法确定内容长度“。你如何在cURL中构造一个oauth头文件?它是否因api而异?根据TradeKing api文档,我需要的是我的用户密钥,访问令牌和用于访问的秘密 - “个人应用”。 (https://developers.tradeking.com/documentation/oauth) – user3806824 2014-11-06 21:47:16

+0

好的,这是进步 - 你现在已经将问题隔离到卷曲问题。你有没有试过curl上的'--ignore-content-length'选项?这是我唯一能想到的其他事情。如果这不起作用,你可能想问一个新的问题,用curl标记,并留下任何提及sas的问题。它可能会引起一些卷毛专家的注意(我不是其中一位)。 – 2014-11-06 23:13:05

+0

我尝试了你建议的命令,而且我仍然遇到同样的错误。我会发布一个新问题。再次感谢你的帮助。 – user3806824 2014-11-07 07:16:07