help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to send a request to a Website.


From: Thierry Leurent
Subject: Re: How to send a request to a Website.
Date: Thu, 20 Apr 2017 23:22:31 +0200
User-agent: KMail/5.2.3 (Linux/4.9.0-2-amd64; KDE/5.28.0; x86_64; ; )

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 via email to

[Prev in Thread] Current Thread [Next in Thread]