#!/usr/bin/env bb
;; -*- clojure -*-
;; deps: { :deps { cheshire/cheshire {:mvn/version "5.10.1"} } }
(require '[babashka.http-client :as http])
(require '[cheshire.core :as json])
;; Retrieve the API token from the environment.
(def token
(or (System/getenv "PAPERTRAIL_TOKEN")
(do
(println "Error: Please set the PAPERTRAIL_TOKEN environment variable.")
(System/exit 1))))
;; Read an optional query argument from the command-line.
(def query (first *command-line-args*))
;; Define the Papertrail events API endpoint.
(def url "https://papertrailapp.com/api/v1/events.json")
;; Build HTTP options with basic authentication (username = token, password = blank).
(def base-opts {:basic-auth [token ""]})
;; If a query was provided, add it as a query parameter "q".
(def opts (if query
(assoc base-opts :query-params {"q" query})
base-opts))
;; Make the HTTP GET request.
(let [resp (http/get url opts)]
(if (= 200 (:status resp))
(let [data (json/parse-string (:body resp) true)
events (:events data)]
(doseq [e events]
;; Print the timestamp and the message for each event.
(println (:received_at e) "->" (:message e))))
(do
(println "Error:" (:status resp) (:body resp))
(System/exit 1))))
当我运行脚本时,它会失败
✗ bb papertrail_logs.bb
----- Error --------------------------------------------------------------------
Type: clojure.lang.ExceptionInfo
Message: Exceptional status code: 401
Data: {:status 401, :body "HTTP Basic: Access denied.\n", :version :http1.1, :headers {"cache-control" "no-cache", "content-length" "27", "content-type" "text/html; charset=utf-8", "date" "Thu, 06 Feb 2025 18:46:00 GMT", "status" "401 Unauthorized", "strict-transport-security" "max-age=31536000", "vary" "Origin", "www-authenticate" "Basic realm=\"Papertrail API\""}, :uri #object[java.net.URI 0x1f2f7832 "https://papertrailapp.com/api/v1/events.json"], :request {:headers {:accept "*/*", :accept-encoding ["gzip" "deflate"], :user-agent "babashka.http-client/0.4.22", :authorization "Basic Wm16b2JwWnJ1ZU4yUHA5ZHNERjo="}, :basic-auth ["ZmzobpZrueN2Pp9dsDF" ""], :uri #object[java.net.URI 0x1f2f7832 "https://papertrailapp.com/api/v1/events.json"], :method :get}}
Location: /Users/pedro/projects/knowledge-base/papertrail_logs.bb:29:12
----- Context ------------------------------------------------------------------
25: (assoc base-opts :query-params {"q" query})
26: base-opts))
27:
28: ;; Make the HTTP GET request.
29: (let [resp (http/get url opts)]
^--- Exceptional status code: 401
30: (if (= 200 (:status resp))
31: (let [data (json/parse-string (:body resp) true)
32: events (:events data)]
33: (doseq [e events]
34: ;; Print the timestamp and the message for each event.
----- Stack trace --------------------------------------------------------------
babashka.http-client.interceptors/fn--28510 - <built-in>
babashka.http-client.internal/then - <built-in>
babashka.http-client.internal/request/fn--28611 - <built-in>
clojure.core/reduce - <built-in>
babashka.http-client.internal/request - <built-in>
babashka.http-client/request - <built-in>
babashka.http-client/get - <built-in>
user - /Users/pedro/projects/knowledge-base/papertrail_logs.bb:29:12
user - /Users/pedro/projects/knowledge-base/papertrail_logs.bb:29:1
是否可以解决如何提供令牌? 我已经验证了设置环境变量,而令牌本身是有效的。 API是否可能需要其他参数或http Basic Auth的标题? 在这种情况下,使用Babashka的HTTP客户端使用Babashka的HTTP客户有任何已知的怪癖吗? 或者我的使用Babashka.http-client可能会有问题吗?
任何帮助或见解将不胜感激!预先感谢我解决了:
#!/usr/bin/env bb
;; -*- clojure -*-
;; deps: { :deps { cheshire/cheshire {:mvn/version "5.10.1"} } }
;; Useful docs: https://www.papertrail.com/help/search-api/
(require '[babashka.http-client :as http])
(require '[cheshire.core :as json])
;; Retrieve the API token from the environment.
(def token
(or (System/getenv "PAPERTRAIL_TOKEN")
(do
(println "Error: Please set the PAPERTRAIL_TOKEN environment variable.")
(System/exit 1))))
;; Read an optional query argument from the command-line.
(def query (first *command-line-args*))
;; Define the Papertrail events API endpoint.
(def url "https://papertrailapp.com/api/v1/events/search.json")
;; https://papertrailapp.com/api/v1/systems.json.
;; Build HTTP options with basic authentication (username = token, password = blank).
(def base-opts {:headers {"X-Papertrail-Token" token}
:query-params {"limit" 10000}})
;; If a query was provided, add it as a query parameter "q".
(def opts (if query
(assoc base-opts :query-params {"q" query})
base-opts))
;; Make the HTTP GET request.
(let [resp (http/get url opts)]
(if (= 200 (:status resp))
(let [data (json/parse-string (:body resp) true)
events (:events data)]
(doseq [e events]
;; Print the timestamp and the message for each event.
(println (:received_at e) "->" (:message e))))
(do
(println "Error:" (:status resp) (:body resp))
(System/exit 1))))