all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to send a request to a Website.
@ 2017-04-20 10:59 Thierry Leurent
  2017-04-20 14:54 ` Eric Abrahamsen
  0 siblings, 1 reply; 7+ messages in thread
From: Thierry Leurent @ 2017-04-20 10:59 UTC (permalink / raw)
  To: help-gnu-emacs@gnu.org

Hello,
Hello,

I'm working on a way to post article to a WordPress blog using OAuth2.
I'm able to retrieve the OAuth2 token but I cannot publish a post using Emacs. 
I'm not allowed, why ???? I don't know but I can do it using Python so I would 
like understant what append.

Thank.

Thierry 


Emacs' Code ---------------
(require 'url)
(require 'url-http)
(require 'json)
(defvar user-data
  (with-current-buffer
      (progn
	(url-request-method        "POST")
	(url-request-extra-headers `(("Content-Type" . "application/x-www-form-
urlencoded")
				     ("Authorization" . "bearer 
1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
	(url-request-data          (json-encode '(:title "Post using emacs.")))
	(url-retrieve-synchronously "https://asgardian.be/WordPress/wp-json/wp/v2/
posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")
	)
    (goto-char url-http-end-of-headers)
    (json-read)
    ))
(message "DBG - Result : %s " user-data)

DBG - Result : ((code . rest_cannot_create) (message . Sorry, you are not 
allowed to create posts as this user.) (data (status . 401))) 

Python's code -------------------
import urllib
import httplib2
import pprint
http = httplib2.Http()
url = 'https://asgardian.be/WordPress/wp-json/wp/v2/posts?
state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt'   
headers = {'Content-type': 'application/x-www-form-urlencoded', 
'Authorization': 'bearer 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt'}
body = {"title":"New post using emacs and python"}
response, content = http.requestb(url, 'POST', headers=headers, 
body=urllib.urlencode(body))
print(" Content : {}" .format(content))

-- 
Thierry Leurent




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Abrahamsen @ 2017-04-20 14:54 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Leurent <thierry.leurent@asgardian.be> writes:


[...]

> Emacs' Code ---------------
> (require 'url)
> (require 'url-http)
> (require 'json)
> (defvar user-data
>   (with-current-buffer
>       (progn
> 	(url-request-method        "POST")
> 	(url-request-extra-headers `(("Content-Type" . "application/x-www-form-
> urlencoded")
> 				     ("Authorization" . "bearer 
> 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
> 	(url-request-data          (json-encode '(:title "Post using emacs.")))
> 	(url-retrieve-synchronously "https://asgardian.be/WordPress/wp-json/wp/v2/
> posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")
> 	)

I'm surprised the above doesn't raise an error: first of all you're also
missing the "buffer" argument to `with-current-buffer'. Then the `progn'
form evaluates each of the forms it contains individually, meaning that
something like (url-request-method "POST") would be seen as a function
call to `url-request-method', which isn't a real function, and should
raise an error. In your progn, the first three forms should be variable
bindings, only the last is an actual function call. So:

(with-current-buffer buffer-name
    (let ((url-request-method "POST")
	  (url-request-extra-headers
	   `(("Content-Type" . "application/x-www-form- urlencoded")
	     ("Authorization" . "bearer 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
	  (url-request-data (json-encode '(:title "Post using emacs."))))
      (url-retrieve-synchronously "https://asgardian.be/WordPress/wp-json/wp/v2/
 posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))


Try that and see if it works.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  2017-04-20 14:54 ` Eric Abrahamsen
@ 2017-04-20 17:02   ` Thierry Leurent
  2017-04-20 17:24     ` Eric Abrahamsen
  0 siblings, 1 reply; 7+ messages in thread
From: Thierry Leurent @ 2017-04-20 17:02 UTC (permalink / raw)
  To: help-gnu-emacs

Thank you your help but it don't work.

It's not a syntax problem. 
The code return a result. IMHO, it's a trouble with the content of the http 
request. 
I'm not an lisp programmer and I have some trouble to understand how the code 
work.

On Thursday, April 20, 2017 7:54:36 AM CEST Eric Abrahamsen wrote:
> Thierry Leurent <thierry.leurent@asgardian.be> writes:
> 
> 
> [...]
> 
> > Emacs' Code ---------------
> > (require 'url)
> > (require 'url-http)
> > (require 'json)
> > (defvar user-data
> > 
> >   (with-current-buffer
> >   
> >       (progn
> > 	
> > 	(url-request-method        "POST")
> > 	(url-request-extra-headers `(("Content-Type" . "application/x-www-form-
> > 
> > urlencoded")
> > 
> > 				     ("Authorization" . "bearer
> > 
> > 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
> > 
> > 	(url-request-data          (json-encode '(:title "Post using emacs.")))
> > 	(url-retrieve-synchronously
> > 	"https://asgardian.be/WordPress/wp-json/wp/v2/
> > 
> > posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")
> > 
> > 	)
> 
> I'm surprised the above doesn't raise an error: first of all you're also
> missing the "buffer" argument to `with-current-buffer'. Then the `progn'
> form evaluates each of the forms it contains individually, meaning that
> something like (url-request-method "POST") would be seen as a function
> call to `url-request-method', which isn't a real function, and should
> raise an error. In your progn, the first three forms should be variable
> bindings, only the last is an actual function call. So:
> 
> (with-current-buffer buffer-name
>     (let ((url-request-method "POST")
> 	  (url-request-extra-headers
> 	   `(("Content-Type" . "application/x-www-form- urlencoded")
> 	     ("Authorization" . "bearer
> 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt"))) (url-request-data (json-encode
> '(:title "Post using emacs.")))) (url-retrieve-synchronously
> "https://asgardian.be/WordPress/wp-json/wp/v2/
> posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
> 
> 
> Try that and see if it works.


-- 
Thierry Leurent




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  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
  0 siblings, 2 replies; 7+ messages in thread
From: Eric Abrahamsen @ 2017-04-20 17:24 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Leurent <thierry.leurent@asgardian.be> writes:

> Thank you your help but it don't work.
>
> It's not a syntax problem. 
> The code return a result. IMHO, it's a trouble with the content of the http 
> request. 
> I'm not an lisp programmer and I have some trouble to understand how the code 
> work.

Okay sorry, I was just fixing the obvious lisp errors in your example, I
didn't actually try the code.

`url-retrieve-synchronously' returns a buffer containing the URL
contents, not the data itself. So it should look like this:

(require 'url)
(require 'url-http)
(require 'json)
(defvar data-buffer
  (let ((url-request-method "POST")
	(url-request-extra-headers
	 `(("Content-Type" . "application/x-www-form-urlencoded")
	   ("Authorization" . "bearer 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
	(url-request-data (json-encode '(:title "Post using emacs."))))
    (url-retrieve-synchronously "https://asgardian.be/WordPress/wp-json/wp/v2/posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
(display-buffer data-buffer)

If there's something wrong with the actual authorization process, of
course, I won't be able to help with that. But to my knowledge the above
should be the equivalent of the Python code you posted.

Eric




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  2017-04-20 17:24     ` Eric Abrahamsen
@ 2017-04-20 18:07       ` Thierry Leurent
  2017-04-20 18:15       ` Kevin Buchs
  1 sibling, 0 replies; 7+ messages in thread
From: Thierry Leurent @ 2017-04-20 18:07 UTC (permalink / raw)
  To: help-gnu-emacs

It's great.... ;). You help-me too understand lisp
On Thursday, April 20, 2017 10:24:25 AM CEST Eric Abrahamsen wrote:
> Thierry Leurent <thierry.leurent@asgardian.be> writes:
> > Thank you your help but it don't work.
> > 
> > It's not a syntax problem.
> > The code return a result. IMHO, it's a trouble with the content of the
> > http
> > request.
> > I'm not an lisp programmer and I have some trouble to understand how the
> > code work.
> 
> Okay sorry, I was just fixing the obvious lisp errors in your example, I
> didn't actually try the code.
> 
> `url-retrieve-synchronously' returns a buffer containing the URL
> contents, not the data itself. So it should look like this:
> 
> (require 'url)
> (require 'url-http)
> (require 'json)
> (defvar data-buffer
>   (let ((url-request-method "POST")
> 	(url-request-extra-headers
> 	 `(("Content-Type" . "application/x-www-form-urlencoded")
> 	   ("Authorization" . "bearer 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
> 	(url-request-data (json-encode '(:title "Post using emacs."))))
>     (url-retrieve-synchronously
> "https://asgardian.be/WordPress/wp-json/wp/v2/posts?state=1234&access_token
> =1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt"))) (display-buffer data-buffer)
> 
> If there's something wrong with the actual authorization process, of
> course, I won't be able to help with that. But to my knowledge the above
> should be the equivalent of the Python code you posted.
> 
> Eric


-- 
Thierry Leurent




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Kevin Buchs @ 2017-04-20 18:15 UTC (permalink / raw)
  To: Eric Abrahamsen, help-gnu-emacs

I was trying to understand how your code could work, but the HTTP 401
status suggested it was working, partially at least. One item that stood
out was that you hardcoded the SAML token. I would expect that would only
expire after some time. So maybe the rest of your code does the token
request and you gave us an example token.

I'd suggest you make the changes Eric proposed and then give us the code
and the response you got. If you are using the token as a variable and not
hardcoded please send corresponding code.



On Thu, Apr 20, 2017 at 10:24 AM Eric Abrahamsen <eric@ericabrahamsen.net>
wrote:

> Thierry Leurent <thierry.leurent@asgardian.be> writes:
>
> > Thank you your help but it don't work.
> >
> > It's not a syntax problem.
> > The code return a result. IMHO, it's a trouble with the content of the
> http
> > request.
> > I'm not an lisp programmer and I have some trouble to understand how the
> code
> > work.
>
> Okay sorry, I was just fixing the obvious lisp errors in your example, I
> didn't actually try the code.
>
> `url-retrieve-synchronously' returns a buffer containing the URL
> contents, not the data itself. So it should look like this:
>
> (require 'url)
> (require 'url-http)
> (require 'json)
> (defvar data-buffer
>   (let ((url-request-method "POST")
>         (url-request-extra-headers
>          `(("Content-Type" . "application/x-www-form-urlencoded")
>            ("Authorization" . "bearer
> 1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt")))
>         (url-request-data (json-encode '(:title "Post using emacs."))))
>     (url-retrieve-synchronously "
> https://asgardian.be/WordPress/wp-json/wp/v2/posts?state=1234&access_token=1zzpkqkabx2v424kjn8yqmfjhywzny6sn2bmb7kt
> ")))
> (display-buffer data-buffer)
>
> If there's something wrong with the actual authorization process, of
> course, I won't be able to help with that. But to my knowledge the above
> should be the equivalent of the Python code you posted.
>
> Eric
>
>
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: How to send a request to a Website.
  2017-04-20 18:15       ` Kevin Buchs
@ 2017-04-20 21:22         ` Thierry Leurent
  0 siblings, 0 replies; 7+ messages in thread
From: Thierry Leurent @ 2017-04-20 21:22 UTC (permalink / raw)
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-04-20 21:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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.