2013-04-29 46 views
2

我使用request.el库(可通过MELPA获得)尝试创建基本框架,从Stack Exchange mode for Emacs开始正式开始工作。我所要做的就是能够将由json-read解析的对象返回给调用函数,但我似乎无法建立连接。Request.el“无法创建与api.stackexchange.com的连接”

据我所知,要让我的函数返回对象,调用必须同步进行,所以这就是:sync t。我曾考虑过把它作为一个异步调用,但我认为考虑它的用例并不会有好处。

起初,在看了这些信息之后,我想'也许我没有必要的二进制文件。'我测试了request.el以及一些与它的文档一起提供的示例调用,并且它们工作正常,所以没有了。

我不知道什么是错的。我还没有很多经验,成功或其他方面的经验,处理网络的任何事情,并没有完全理解我得到的错误消息。据我所知,API的443端口给了我沉默的待遇,但我很犹豫,认为是这种情况;)

;; Works like a charm 
(defun alist-to-json (alist) 
    "Converts the key-value pairs of `ALIST` into a JSON-friendly 
string: \"key1=value1&key2=value2&\"." 
    (apply 'concat 
    (mapcar (lambda (kv) 
       (format "%s=%s&" (car kv) 
         (if (stringp (cdr kv)) 
          (cdr kv) 
         (number-to-string (cdr kv))))) 
      alist))) 


(defvar stack-api-root "https://api.stackexchange.com/2.1/") 

(require 'json) 
(require 'request) 

(defun stack-api-request (call keys-alist) 
    "Makes the specified `CALL` to the Stack Exchange API with the 
    key-value pairs given `KEYS-ALIST`. For example, 

    (stack-api-request \"sites\" '((page . 2) (page_size . 25)))" 

    (let* ((base-call (concat stack-api-root call "?")) 
     (options (alist-to-json keys-alist))) 
    (request base-call 
    :params options 
    :parser 'json-read 
    :sync t))) 

回溯

Debugger entered--Lisp error: (error "Could not create connection to api.stackexchange.com:443") 
    signal(error ("Could not create connection to api.stackexchange.com:443")) 
    error("Could not create connection to %s:%d" "api.stackexchange.com" 443) 
    url-http([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil)) 
    url-https([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil)) 
    url-retrieve-internal("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil) nil nil) 
    url-retrieve("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"]) 
    url-retrieve-synchronously("https://api.stackexchange.com/2.1/sites?&") 
    request--url-retrieve-sync("https://api.stackexchange.com/2.1/sites?&" :params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response #0) nil nil nil url-retrieve nil]) 
    apply(request--url-retrieve-sync "https://api.stackexchange.com/2.1/sites?&" (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil #0 nil nil nil url-retrieve nil])) 
    request("https://api.stackexchange.com/2.1/sites?" :params "page=2&page_size=25&" :parser json-read :sync t) 
    (let* ((base-call (concat stack-api-root call "?")) (options (alist-to-json keys-alist))) (request base-call :params options :parser (quote json-read) :sync t)) 
    stack-api-request("sites" ((page . 2) (page_size . 25))) 
    eval((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil) 
    eval-expression((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil) 
    call-interactively(eval-expression nil nil) 

消息:

Contacting host: api.stackexchange.com:443 
Opening TLS connection to `api.stackexchange.com'... 
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com'...failed 
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com --protocols ssl3'...failed 
Opening TLS connection with `openssl s_client -connect api.stackexchange.com:443 -no_ssl2 -ign_eof'...failed 
Opening TLS connection to `api.stackexchange.com'...failed 

I checked,以确保这不符合卷曲问题。我用curl电话是

curl api.stackexchange.com/2.1/sites --compressed 

从外面看进request库,request的那样好这样做。我不知道会发生什么问题。

+0

我没有时间仔细查看问题,但是您是否使用卷曲后端检查它?也许这是另一个url.el的错误。如果你有卷曲(命令行一,而不是libcurl),它将被自动使用。我建议通过url.el来使用它。 – tkf 2013-04-30 21:40:04

+0

@tkf首先感谢您的阅读。我已经忘记了我正在一台瘫痪的计算机上(windows,没有cygwin/admin;非常漫长而悲伤的故事),并且将研究curl业务 - 我想我可以使用Windows的独立版本,而且我将会报告更新。 – 2013-04-30 21:42:49

+0

“REQUEST [错误]连接到... api .../sites时出错(错误)”这是很多错误,但代码是60(并且,假设它是'curl'错误,“对等证书不能“)鉴于我获得了不同的结果,我认为我做得对,我无法在Windows端口和url.el之间获胜。 – 2013-04-30 21:58:04

回答

1

我把你的例子简化成下面的代码片段来重现你的问题,但它确实有效。你可以试试这个吗?

(request 
"http://api.stackexchange.com/2.1/sites" 
:parser 'json-read 
:params '((page . "2") (page_size . "25")) 
:success (lambda (&rest args) (princ (plist-get args :data)))) 

它应该打印一些数据到*Messages*缓冲区和回波区域。

编辑:你的例子中的问题似乎是你要传递字符串到PARAMS只需要alist。我将更改代码以引发错误,以便更易于调试。

+0

从[文档](http://tkf.github.io/emacs-request/)中的POST示例中,字符串应该是等效的,否? – 2013-05-05 10:59:46

+0

DATA允许字符串,但不允许PARAMS。 PARAMS去url和DATA模仿HTML表单等等。但是PARAMS应该接受字符串。 – tkf 2013-05-05 13:54:32

+0

啊,我现在明白了。那里有两个不同的论点感谢您的支持! – 2013-05-05 17:12:59