Unterschiede zwischen den Revisionen 2 und 3
Revision 2 vom 2016-11-08 19:59:27
Größe: 1152
Autor: anonym
Kommentar:
Revision 3 vom 2016-12-07 15:21:23
Größe: 6085
Autor: anonym
Kommentar:
Gelöschter Text ist auf diese Art markiert. Hinzugefügter Text ist auf diese Art markiert.
Zeile 7: Zeile 7:
 * installieren & aktivieren: {{{   * installieren & aktivieren:
{{{
Zeile 15: Zeile 16:
 * neuen Service anlegen: {{{   * neuen Service anlegen:
{{{
Zeile 24: Zeile 26:
= REST API in Wordpress 4.6 =
 * http://v2.wp-api.org/
 * https://github.com/WP-API/OAuth1
 * ...
= REST API in Wordpress =
 * bis Wordpress 4.6 als Plugin installieren: http://v2.wp-api.org/
  * seit WP 4.7 steckt der REST Code im Core
   * follow the ongoing process: https://make.wordpress.org/core/tag/rest-api/
  * Standardpfad für REST Dinge: ''/wp-json/wp/v2/''
 * OAuth Wordpress Plugin: https://github.com/WP-API/OAuth1
  * benötigt htaccess rewrite rule: https://wp-oauth.com/kb/client-credentials-not-found-headers-body/ {{{
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
}}}
Zeile 29: Zeile 36:
= Python =
 * python3-requests
 * python3-requests-oauthlib
 * http://requests-oauthlib.readthedocs.io/en/latest/oauth1_workflow.html
Zeile 36: Zeile 39:


= Python =
 * Debian Pakete installieren:
  * python3-requests (http://docs.python-requests.org/)
  * python3-requests-oauthlib (https://requests-oauthlib.readthedocs.io/en/latest/)
  * in python: {{{
import requests
from requests_oauthlib import OAuth1Session
}}}

== Python REST Beispiele ==
=== Vorbereitung für oauth1 ===
{{{
def oauth1_threeleggedflow():
    """ for three legged oauth follow the docs at:
    http://requests-oauthlib.readthedocs.io/en/latest/oauth1_workflow.html
    """
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    callback = config['oauth']['callback']
    oauth = OAuth1Session(client_key=oauthkey,
                          client_secret=oauthpass, callback_uri=callback)
    logging.debug("open OAuth session")

    """ 1.) request token runterladen
    """
    fetch_response = oauth.fetch_request_token(config['oauth']['request'],
                                               oauthkey)
    resource_owner_key = fetch_response.get('oauth_token')
    logging.debug(resource_owner_key)
    resource_owner_secret = fetch_response.get('oauth_token_secret')
    logging.debug(resource_owner_secret)

    """ 2.) durch user authorisieren lassen
    und damit den verifier link zum access token bekommen
    """
    authorization_url = oauth.authorization_url(config['oauth']['authorize'])
    print('Please login to wordpress go there and authorize: '
          + authorization_url)
    redirect_response = input('Afterwards paste the full redirect URL here: ')
    oauth_response = oauth.parse_authorization_response(redirect_response)
    logging.debug(oauth_response)
    oauth_verifier = oauth_response.get('oauth_verifier')
    logging.warning(oauth_verifier)

    """ 3.) access token holen
    """
    oauth = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=resource_owner_key,
                          resource_owner_secret=resource_owner_secret,
                          verifier=oauth_verifier)
    oauth_tokens = oauth.fetch_access_token(config['oauth']['access'])
    # gut merken...
    oauth_token = oauth_tokens.get('oauth_token')
    logging.info(oauth_token)
    # gut merken...
    oauth_token_secret = oauth_tokens.get('oauth_token_secret')
    logging.info(oauth_token_secret)
}}}

=== Wordpress Post erstellen ===
{{{
def post_content(payload):
    """ post the given payload as wp event
    """
    domain = config['website']['domain']
    postpath = config['website']['postpath']
    url = domain + postpath
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    oauth_token = config['oauth']['token']
    oauth_token_secret = config['oauth']['secret']
    head = {'Content-Type': 'application/json',
            'user-agent': 'RESTeRampe'}
    pwhwp = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=oauth_token,
                          resource_owner_secret=oauth_token_secret)
    r = pwhwp.post(url, json=payload, headers=head)
    logging.info("HTTP status: " + str(r.status_code) + "\n")
    logging.info("HTTP json: " + str(r.json()))
}}}

=== Bild zu Wordpress hochladen ===
{{{
def post_image(imagepath):
    """ upload image to wordpress site
    """
    domain = config['website']['domain']
    imagepostpath = config['website']['imagepostpath']
    url = domain + imagepostpath
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    oauth_token = config['oauth']['token']
    oauth_token_secret = config['oauth']['secret']
    path, filename = os.path.split(imagepath)
    head = {'user-agent': 'RESTeRampe',
            'Cache-Control': 'no-cache',
            'Content-Type': 'image/jpeg',
            'Content-Disposition': 'attachment; filename=' + filename
            }
    payload = {'status': 'publish' # set the event as public in wordpress
            }
    pwhwp = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=oauth_token,
                          resource_owner_secret=oauth_token_secret)
    with open(imagepath, "rb") as image:
        data = image.read()
    r = pwhwp.post(url, data=data, headers=head)
    logging.info("HTTP status: " + str(r.status_code) + "\n")
    logging.info("HTTP json: " + str(r.json()))
    image_id = r.json()['id'] # save id of new uploaded image
    logging.info("wp image id: %s", str(image_id))
    return image_id
}}}

Inhalte aus Drupal sollen mittels REST zu Wordpress übetragen werden. Ein kleines Python Script erledigt den Abgleich.

REST API in Drupal 7 mittels Services

Drupal 8 hat die REST API im Kern, bei Drupal 7 lässt sich das mit dem services Modul nachinstallieren.

  • https://www.ostraining.com/blog/drupal/services/

  • installieren & aktivieren:

    aptitude install php7.0-curl
    drush dl libraries services services_views oauth
    drush en libraries services services_views oauth
    drush make --no-core sites/all/modules/services/services.make sites/all/libraries/
  • neuen View als "Service" anlegen
    • URL festlegen
  • neuen Service anlegen:
    http://.../admin/structure/services/add
    • URL eintragen
    • Berechtigungen festlegen

Aktion bei Seitenänderung ausführen

  • rules Modul

REST API in Wordpress

Webclient

Python

Python REST Beispiele

Vorbereitung für oauth1

def oauth1_threeleggedflow():
    """ for three legged oauth follow the docs at:
    http://requests-oauthlib.readthedocs.io/en/latest/oauth1_workflow.html
    """
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    callback = config['oauth']['callback']
    oauth = OAuth1Session(client_key=oauthkey,
                          client_secret=oauthpass, callback_uri=callback)
    logging.debug("open OAuth session")

    """ 1.) request token runterladen
    """
    fetch_response = oauth.fetch_request_token(config['oauth']['request'],
                                               oauthkey)
    resource_owner_key = fetch_response.get('oauth_token')
    logging.debug(resource_owner_key)
    resource_owner_secret = fetch_response.get('oauth_token_secret')
    logging.debug(resource_owner_secret)

    """ 2.) durch user authorisieren lassen
    und damit den verifier link zum access token bekommen
    """
    authorization_url = oauth.authorization_url(config['oauth']['authorize'])
    print('Please login to wordpress go there and authorize: '
          + authorization_url)
    redirect_response = input('Afterwards paste the full redirect URL here: ')
    oauth_response = oauth.parse_authorization_response(redirect_response)
    logging.debug(oauth_response)
    oauth_verifier = oauth_response.get('oauth_verifier')
    logging.warning(oauth_verifier)

    """ 3.) access token holen
    """
    oauth = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=resource_owner_key,
                          resource_owner_secret=resource_owner_secret,
                          verifier=oauth_verifier)
    oauth_tokens = oauth.fetch_access_token(config['oauth']['access'])
    # gut merken...
    oauth_token = oauth_tokens.get('oauth_token')
    logging.info(oauth_token)
    # gut merken...
    oauth_token_secret = oauth_tokens.get('oauth_token_secret')
    logging.info(oauth_token_secret)

Wordpress Post erstellen

def post_content(payload):
    """ post the given payload as wp event
    """
    domain = config['website']['domain']
    postpath = config['website']['postpath']
    url = domain + postpath
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    oauth_token = config['oauth']['token']
    oauth_token_secret = config['oauth']['secret']
    head = {'Content-Type': 'application/json',
            'user-agent': 'RESTeRampe'}
    pwhwp = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=oauth_token,
                          resource_owner_secret=oauth_token_secret)
    r = pwhwp.post(url, json=payload, headers=head)
    logging.info("HTTP status: " + str(r.status_code) + "\n")
    logging.info("HTTP json: " + str(r.json()))

Bild zu Wordpress hochladen

def post_image(imagepath):
    """ upload image to wordpress site
    """
    domain = config['website']['domain']
    imagepostpath = config['website']['imagepostpath']
    url = domain + imagepostpath
    oauthkey = config['oauth']['key']
    oauthpass = config['oauth']['pw']
    oauth_token = config['oauth']['token']
    oauth_token_secret = config['oauth']['secret']
    path, filename = os.path.split(imagepath)
    head = {'user-agent': 'RESTeRampe',
            'Cache-Control': 'no-cache',
            'Content-Type': 'image/jpeg',
            'Content-Disposition': 'attachment; filename=' + filename
            }
    payload = {'status': 'publish' # set the event as public in wordpress
            }
    pwhwp = OAuth1Session(oauthkey,
                          client_secret=oauthpass,
                          resource_owner_key=oauth_token,
                          resource_owner_secret=oauth_token_secret)
    with open(imagepath, "rb") as image:
        data = image.read()
    r = pwhwp.post(url, data=data, headers=head)
    logging.info("HTTP status: " + str(r.status_code) + "\n")
    logging.info("HTTP json: " + str(r.json()))
    image_id = r.json()['id'] # save id of new uploaded image
    logging.info("wp image id: %s", str(image_id))
    return image_id

DrupalWordpressREST (zuletzt geändert am 2019-03-10 18:42:49 durch anonym)


Creative Commons Lizenzvertrag
This page is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.