From: Ricardo Wurmus <rekado@elephly.net>
To: Mortimer Cladwell <mbcladwell@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: http-request bearer token syntax for Twitter v2 api
Date: Thu, 13 Oct 2022 13:37:56 +0200 [thread overview]
Message-ID: <87h708gdfg.fsf@elephly.net> (raw)
In-Reply-To: <CAOcxjM5QguZtLEEZyF0j7QH7031gJ6bU2cxhPWSq7g+gz3_fHA@mail.gmail.com>
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 “bearer”
method with a single string. It expects that authentication methods
other than Basic always provide key=value 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) default-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 (= 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=created_at&expansions=author_id&user.fields=created_at&max_results=5")
(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.
--
Ricardo
next prev parent reply other threads:[~2022-10-13 11:37 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-12 10:30 http-request bearer token syntax for Twitter v2 api Mortimer Cladwell
2022-10-12 11:06 ` Olivier Dion via General Guile related discussions
2022-10-12 18:33 ` Mortimer Cladwell
2022-10-13 5:53 ` James Crake-Merani
2022-10-13 8:53 ` Maxime Devos
2022-10-13 11:37 ` Ricardo Wurmus [this message]
2022-10-13 20:42 ` Mortimer Cladwell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87h708gdfg.fsf@elephly.net \
--to=rekado@elephly.net \
--cc=guile-user@gnu.org \
--cc=mbcladwell@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).