From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Thierry Leurent Newsgroups: gmane.emacs.help Subject: Re: How to send a request to a Website. Date: Thu, 20 Apr 2017 23:22:31 +0200 Message-ID: <4426483.ga5oOEorW7@e6430> References: <2320801.DznD1DNTWb@e6430> <87o9vr0z7a.fsf@ericabrahamsen.net> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: blaine.gmane.org 1492723395 19676 195.159.176.226 (20 Apr 2017 21:23:15 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 20 Apr 2017 21:23:15 +0000 (UTC) User-Agent: KMail/5.2.3 (Linux/4.9.0-2-amd64; KDE/5.28.0; x86_64; ; ) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Apr 20 23:23:11 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d1JXj-0004sY-Ls for geh-help-gnu-emacs@m.gmane.org; Thu, 20 Apr 2017 23:23:11 +0200 Original-Received: from localhost ([::1]:56072 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1JXk-0001Ur-Gx for geh-help-gnu-emacs@m.gmane.org; Thu, 20 Apr 2017 17:23:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58870) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d1JXD-0001Ub-11 for help-gnu-emacs@gnu.org; Thu, 20 Apr 2017 17:22:41 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d1JX9-0005AO-Nl for help-gnu-emacs@gnu.org; Thu, 20 Apr 2017 17:22:39 -0400 Original-Received: from 8.mo3.mail-out.ovh.net ([87.98.172.249]:58909) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d1JX9-00059W-6N for help-gnu-emacs@gnu.org; Thu, 20 Apr 2017 17:22:35 -0400 Original-Received: from player758.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo3.mail-out.ovh.net (Postfix) with ESMTP id 47AC7CA882 for ; Thu, 20 Apr 2017 23:22:32 +0200 (CEST) Original-Received: from e6430.localnet (200.27-200-80.adsl-dyn.isp.belgacom.be [80.200.27.200]) (Authenticated sender: thierry.leurent@asgardian.be) by player758.ha.ovh.net (Postfix) with ESMTPSA id DA4432C007D for ; Thu, 20 Apr 2017 23:22:31 +0200 (CEST) In-Reply-To: X-Ovh-Tracer-Id: 2780972770162389974 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeeliedrfeefgddtvdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemuceftddtnecu X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 87.98.172.249 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:112828 Archived-At: 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 "

A quick and dirty post.

\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.