From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.user Subject: Re: http-request bearer token syntax for Twitter v2 api Date: Thu, 13 Oct 2022 13:37:56 +0200 Message-ID: <87h708gdfg.fsf@elephly.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11292"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.8.9; emacs 28.1 Cc: guile-user@gnu.org To: Mortimer Cladwell Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Oct 13 13:43:30 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oiwcb-0002jT-Ci for guile-user@m.gmane-mx.org; Thu, 13 Oct 2022 13:43:29 +0200 Original-Received: from localhost ([::1]:53168 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oiwcZ-0005jv-Vc for guile-user@m.gmane-mx.org; Thu, 13 Oct 2022 07:43:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50170) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiwZF-0005ie-Ax for guile-user@gnu.org; Thu, 13 Oct 2022 07:40:08 -0400 Original-Received: from sender4-of-o51.zoho.com ([136.143.188.51]:21127) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiwZC-0003ME-5x for guile-user@gnu.org; Thu, 13 Oct 2022 07:40:00 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1665661193; cv=none; d=zohomail.com; s=zohoarc; b=b8MOvbS/OLDXyRtGgB0nMAb2E9FdIvW1lnGVW2g928ebhfhQpGPoGiPq537XkJjhUgPOXNObsvY2NaRgRf80Xs96xbQnYvdcV0xZklmZZRVXK+VoDFGI3xvZjc4LZKVxOnQ0bvyDnqEukWta0h6UsrsgBCLO7JS+4C0K8yMKfoE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665661193; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=SVSLJs2Yn0vvvrc3prt8Xo+2lZY7JqtCUzBI604ISww=; b=UGt6J4lEKyeJ7E+4puBgKmbeie4AwNcpj5KVY6NTK9w8dZFA+bACf1WaV2NMTd/qK8GXtxavmVe/p51oLI7seFYOpT7xkehTbVvT7ZVa/ZQn35OCOSe1ED6VTJDTzjzwwqu9FmsMsR+1rVwvSjEjQxE6yMqWVmXHtvb5/Uw8Ff4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1665661193; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:In-reply-to:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=SVSLJs2Yn0vvvrc3prt8Xo+2lZY7JqtCUzBI604ISww=; b=ICgfvKkcVZv1xnzQAgxaXFH/PPvDHvJEyS3K0UG9c7GHFuntHSKzxZj77eMLUQzc CF+NUarxqwebfvzWoB50ff8gYLopAcUPfvRorQzWz3tAbBEvprDnnG8x8xP9qdEN+B4 ei3UM+lkZtq9AJC/RjlIP1qmjL6E/TrQCT2T+qJY= Original-Received: from localhost (73-110-142-46.pool.kielnet.net [46.142.110.73]) by mx.zohomail.com with SMTPS id 1665661190835454.0121752262115; Thu, 13 Oct 2022 04:39:50 -0700 (PDT) In-reply-to: X-URL: https://elephly.net X-PGP-Key: https://elephly.net/rekado.pubkey X-PGP-Fingerprint: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC X-ZohoMailClient: External Received-SPF: pass client-ip=136.143.188.51; envelope-from=rekado@elephly.net; helo=sender4-of-o51.zoho.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18638 Archived-At: Hi Mortimer, > Has anyone successfully submitted a bearer token to Twitter v2 api using > http-request? What syntax did you use? The validator for the Authorization header does not permit the =E2=80=9Cbea= rer=E2=80=9D method with a single string. It expects that authentication methods other than Basic always provide key=3Dvalue arguments. So we have to overwrite things: --8<---------------cut here---------------start------------->8--- (import (ice-9 pretty-print) (ice-9 match) (json) (rnrs bytevectors) (web client) (web http) (web request) (web response)) (define* (parse-credentials str #:optional (val-parser (@@ (web http) defau= lt-val-parser)) (start 0) (end (string-length str))) (let* ((start ((@@ (web http) skip-whitespace) str start end)) (delim (or (string-index str char-set:whitespace start end) end))) (when (=3D start end) ((@@ (web http) bad-header-component) 'authorization str)) (let ((scheme (string->symbol (string-downcase (substring str start (or delim end)))))) (case scheme ((bearer) (let* ((start ((@@ (web http) skip-whitespace) str delim end))) (unless (< start end) (pk 'oh start end str) ((@@ (web http) bad-header-component) 'token str)) (cons scheme (substring str start end)))) (else ((@@ (web http) parse-credentials) str val-parser start end)))))) (define (validate-credentials val) (match val (((or 'basic 'bearer) . (? string?)) #t) (((? symbol?) . (? (@@ (web http) key-value-list?))) #t) (_ #f))) (define put-string (@@ (web http) put-string)) (define (write-credentials val port) (match val (('bearer . cred) (put-string port "bearer ") (put-string port cred)) (_ ((@@ (web http) write-credentials) val port)))) (declare-header! "authorization" parse-credentials validate-credentials write-credentials) (let* ((uri "https://api.twitter.com/2/users/2244994945/tweets?tweet.fields= =3Dcreated_at&expansions=3Dauthor_id&user.fields=3Dcreated_at&max_results= =3D5") (my-token "AAA...") (my-headers `((Content-type . "application/json") (authorization . (bearer . ,my-token))))) (call-with-values (lambda () (http-get uri #:headers my-headers)) (lambda (response body) (pretty-print response) (pretty-print (json-string->scm (utf8->string body)))))) --8<---------------cut here---------------end--------------->8--- Note that the POST endpoint for submitting tweets does not allow bearer token authentication. --=20 Ricardo