-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How auth can be added to the request, and reuse the token? #73
Comments
I don't think it is possible to split the raw data into multiple lines like you have. When I copy that Verb block as curl 'http://localhost:8081/backend/rest/v2/oauth/token' \
-H 'Authorization: Basic YXBwYnV5YmFja2VuZC1lcnZEVUdmUjo5YTZhMzUzMjMzZTA4MDgxZDcyMWJjZWM3NDkwOTNhZGU5MTcyZGZkNWM3ZmViYjgxMjc4OWM2ZGRkZmVhMzYy' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-X POST \
--data-raw 'username=admin
password=admin
grant_type=password ' You could join it all on one line using
Or, more readably in my opinion, use an Elisp snippet:
(But the snippet needs to be all on one line; you could probably break it up using a noweb reference if you have a lot more data to include.) Either of these give a correct looking command when copied to curl 'http://localhost:8081/backend/rest/v2/oauth/token' \
-H 'Authorization: Basic YXBwYnV5YmFja2VuZC1lcnZEVUdmUjo5YTZhMzUzMjMzZTA4MDgxZDcyMWJjZWM3NDkwOTNhZGU5MTcyZGZkNWM3ZmViYjgxMjc4OWM2ZGRkZmVhMzYy' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-X POST \
--data-raw 'username=admin&password=admin&grant_type=password' |
Regarding re-using the token, here is how I've been doing that mostly automatically: This code block will let us extract elements from the JSON of a response: #+name: extract-http-response-json
#+begin_src emacs-lisp
;; Search for the JSON contents by looking for a pair of curly braces;
;; if unexpected behaviour occurs, check if the response has extra braces, and if so, this might need to be made smarter
(let ((resp-json (if (string-match "{[^b-a]*}" response) ;; Silly regexp to match all characters including newline (`b-a` is an empty range, so ^-ing it matches all characters)
(match-string 0 response))))
(apply 'verb-json-get resp-json token-path))
#+end_src Then this code block uses the above to extract the access and refresh tokens specifically: #+name: extract-auth-tokens
#+begin_src emacs-lisp :var response="" :noweb yes :noweb-prefix no
(verb-var auth-token "") ;; Verb variable must be set before using verb-set-var, so set it to a default "" if not set yet
(let ((token-path '("access_token")))
(verb-set-var "auth-token" <<extract-http-response-json>>))
(verb-var refresh-token "")
(let ((token-path '("refresh_token")))
(verb-set-var "refresh-token" <<extract-http-response-json>>))
;; Echo the raw response for the results block, followed by info on the extracted values
(concat response "\n\n" (format "extracted keycloak-auth-token: %s\nextracted keycloak-refresh-token: %s"
(verb-var auth-token) (verb-var refresh-token)))
#+end_src Add the below
And then you can use the auth token in all your requests by setting it in a top-level heading, and putting all requests that require authentication under that top-level heading:
|
I don't think there's anything to add to this issue, I will leave the following thoughts:
|
Hi,
I'm struggling with this without success. Any sample I see related to verb or restclient use auth.
I'm trying to migrate from Insomnia to eMacs with ORG+verb
I'm starting to the login API request. I have to do a POST request to http://localhost:8081/backend/rest/v2/oauth/token
The cUrl command is as follows:
And this is what I have
But the API always tells me:
and I expect to get something like:
Where I would like to get the access_token to use it in other requests as Bearer.
One thing I do on Insomnia, and I don't know if could be possible here, is that if I call another request, getToken will be called to request a token if necessary.
Thanks.
The text was updated successfully, but these errors were encountered: