在sas中使用curl:来自api的oauth请求

时间:2014-11-01 07:32:10

标签: api curl sas

我试图在SAS中使用curl从api下载数据。我有一个Consumer键,Consumer secret,OAuth Token,& OAuth Token Secret。似乎我可以连接到api,但是我收到错误声明" HTTP传输:无法确定内容长度"。有没有人有任何见解或想法?非常感谢。

代码:

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

1 个答案:

答案 0 :(得分:0)

要做的第一件事是尝试从命令行运行cURL语句。如果您收到有效回复,则表示存在SAS问题,否则您的cURL请求会出现问题。

您是否尝试在查看https请求时将-k选项添加到cURL语句中?

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