Skip to content

Inject github API token in headers for gists #57

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions webpaste.el
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ This uses `browse-url-generic' to open URLs."

("gist.github.com"
:uri "https://api.github.com/gists"
:headers-lambda webpaste--provider-gist-headers
:post-field nil
:post-field-lambda (lambda () (cl-function (lambda (&key text &allow-other-keys)
(let ((filename (if (buffer-file-name)
Expand Down Expand Up @@ -321,6 +322,25 @@ This is the default failover hook that we use for most providers."
(webpaste--return-url (cdr (assq 'link (json-read-from-string data)))))))


(cl-defun webpaste--provider-gist-headers ()
"Provide headers for gist API authentication.

This expects your authinfo to contain authorization for using gist API.
You can generate your token at https://github.com/settings/tokens
Only gist scope is necessary.

Then save the token in your authinfo as:
machine api.github.com/gists login webpaste password <token>"
(cl-function (lambda ()
(cons `("Authorization"
.
,(concat "token "
(auth-source-pick-first-password
:host "api.github.com/gists"
:login "webpaste")))
nil))))




(cl-defun webpaste--get-lang-alist-with-overrides (overrides)
Expand All @@ -344,6 +364,7 @@ This is the default failover hook that we use for most providers."
(post-data '())
(post-lang-field-name nil)
(parser 'buffer-string)
(headers-lambda nil)
(lang-overrides '())
(lang-uri-separator nil)
(error-lambda 'webpaste--providers-error-lambda)
Expand Down Expand Up @@ -390,6 +411,10 @@ Optional params:
`webpaste--providers-error-lambda-no-failover' available if
you need a provider that isn't allowed to failover.

:headers-lambda Function that builds and returns headers that should be sent
to the provider. This can be useful to add authentication
for example.

:post-field-lambda Function that builds and returns the post data that should be
sent to the provider. It should accept named parameters by
the names TEXT, POST-FIELD and POST-DATA. POST-DATA should
Expand Down Expand Up @@ -432,6 +457,7 @@ Optional params:
:post-lang-field-name post-lang-field-name
:post-data post-data)
:parser parser
:headers (when headers-lambda (funcall (funcall headers-lambda)))
:success (funcall success-lambda)
:sync sync
:error (funcall error-lambda :text text))))))
Expand Down