all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Thierry Leurent <thierry.leurent@asgardian.be>
To: help-gnu-emacs@gnu.org
Subject: Re: How to send a request to a Website.
Date: Thu, 20 Apr 2017 23:22:31 +0200	[thread overview]
Message-ID: <4426483.ga5oOEorW7@e6430> (raw)
In-Reply-To: <CAKT9s6DTYEH=3En89ZeStqhiSj6x=C5OVf+gyr8Qh=ZqdYnnqg@mail.gmail.com>

To understand my path.
How it work :
- In a browser, you put this kind of URL : https://MyWordPressSite/oauth/authorize/?
client_id=CLIENT_ID&client_secret=CLIENT_SECRET&redirect_uri=https://
MyWordPressSite&response_type=code&state=TRANSACTION_ID

-In the new URL in your browser, copy the authenticate code (code=).
-Convert authenticate code to authenticate token. Use this kind of command curl -X POST -d 
'code=TRANSACTION_CODE&grant_type=authorization_code&redirect_uri=https://
MyWordPressSite&client_id=CLIENT_ID&code_verifier=TRANSACTION_ID&client_id=CLIENT_
ID&client_secret=CLIENT_SECRET



First, I used curl to get a token and Python to post an article. I can see the post in my blog 
management page.

After, I find OAuth2-mode for Emacs. I can :
- Get the authentication code.
- Get the token using the authentication code.
- Post using my Python code.

Now, understand the problem.
The code without the good Client ID and secret

(eval-when-compile (require 'cl))
(require 'oauth2)
(require 'json)


(defvar org2blog/wp-server-url nil
  "Weblog server URL.")
(defvar org2blog/wp-oauth2-url nil
  "Weblog OAuth2 URL.")
(defvar org2blog/wp-oauth2-ClientID nil
  "Weblog OAuth2 ClientID.")
(defvar org2blog/wp-oauth2-ClientSecret nil
  "Weblog OAuth2 ClientSecret.")
(defvar org2blog/wp-oauth2-state nil
  "Weblog OAuth2 state.")
(defvar org2blog/wp-token nil
  "Weblog OAuth2 token.")


(setq org2blog/wp-server-url "https://asgardian.be/WordPress")
(setq org2blog/wp-oauth2-path "https://asgardian.be/WordPress/oauth/authorize")
(setq org2blog/wp-oauth2-ClientID "oN7mfeYcBbSO4pvGQVqHotDPSl8yrZ")
(setq org2blog/wp-oauth2-ClientSecret "sQzAdIPbVqnOfrdK83GzL52oUVK3uc")
(setq org2blog/wp-oauth2-state "1234")



(setq url 
      "https://asgardian.be/WordPress/wp-json/wp/v2/posts?state=1234&access_token=")

;; Good code.
;;Get OAuth2 token.
(message "-------")
(message "DBG - Get TOKEN.")
(setq org2blog/wp-token (oauth2-auth
			 "https://asgardian.be/WordPress/oauth/authorize"
			 "https://asgardian.be/WordPress/oauth/token"
			 org2blog/wp-oauth2-ClientID
			 org2blog/wp-oauth2-ClientSecret
			 ""
			 org2blog/wp-oauth2-state
			 org2blog/wp-server-url))
(message "DBG - access-token : %s" (oauth2-token-access-token org2blog/wp-token))

(setq url (concat url  (oauth2-token-access-token org2blog/wp-token)))
(message "DBG - Post Article.")

;;(message "Test oauth2-extra-headers %s" (oauth2-extra-headers '(("Content-Type" . 
"application/x-www-form-urlencoded"))))
(message "Using OAuth2.")
(defvar user-data
  (with-current-buffer
      (oauth2-url-retrieve-synchronously org2blog/wp-token
					 url
					 "POST"
					 (json-encode '(:title "Post 
using emacs." :type "post" :content "<p>A quick and dirty post.</p>\n"))
					 ;;'(("Content-Type" . 
"application/json"))
					 '(("Content-Type" . 
"application/x-www-form-urlencoded"))
					 )
    (goto-char url-http-end-of-headers)
    (json-read)))

(message "DBG - Result : %s " user-data)


(message "Using code from Eric.")
(defvar user-data (with-current-buffer "*Messages*"
		    (let ((url-request-method "POST")
			  (url-request-extra-headers
			   (oauth2-extra-headers '(("Content-Type" . 
"application/x-www-form-urlencoded"))))
			  (url-request-data (json-encode '(:title "Post using 
emacs."))))
		      (url-retrieve-synchronously url)))
  )
(message "DBG - Result : %s " user-data)

The result in *Message*
-------
DBG - Get TOKEN.
Contacting host: asgardian.be:443
DBG - access-token : 3ben3qqjtjrxidf6drhhhbigcygicsbkbfffsa6j
DBG - Post Article.
Using OAuth2.


      reply	other threads:[~2017-04-20 21:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-20 10:59 How to send a request to a Website Thierry Leurent
2017-04-20 14:54 ` Eric Abrahamsen
2017-04-20 17:02   ` Thierry Leurent
2017-04-20 17:24     ` Eric Abrahamsen
2017-04-20 18:07       ` Thierry Leurent
2017-04-20 18:15       ` Kevin Buchs
2017-04-20 21:22         ` Thierry Leurent [this message]

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4426483.ga5oOEorW7@e6430 \
    --to=thierry.leurent@asgardian.be \
    --cc=help-gnu-emacs@gnu.org \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.