2

我正在使用该库(可通过 MELPA 获得)来尝试创建一个基本框架,以便在Emacs 的 Stack Exchange 模式request.el上认真开始工作。我想要做的就是能够将解析的对象返回给调用函数,但我什至似乎无法建立连接。json-read

我知道要让我的函数返回对象,必须同步进行调用,这: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

我检查以确保这不是 cURL 的问题。我使用的电话curl

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

从外部查看request存储库,request也正在这样做。我不知道可能出了什么问题。

4

1 回答 1

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。我将更改代码以引发错误,以便更容易调试。

于 2013-05-04T23:50:44.627 回答