Comment utiliser l'authentification OAuth avec l'API REST via les commandes CURL?
-
-
Les chosesne sontpas sifaciles.J'aiessayé d'écrire une réponsemais c'est assez long.Vouspouvez commencerpar lire la documentation,spécialement [The Authorization Flow] (https://github.com/WP-API/OAuth1/blob/master/docs/basics/Auth-Flow.md).Cemessage a également un [excellenttutoriel] (https://timnash.co.uk/wordpress-rest-api-authentication-examples/).Things are not so easy. I've been trying to write an answer but it is quite long. You could start by reading the docs, specially [The Authorization Flow](https://github.com/WP-API/OAuth1/blob/master/docs/basics/Auth-Flow.md). This posts has also a [great tutorial](https://timnash.co.uk/wordpress-rest-api-authentication-examples/).
- 2
- 2016-01-01
- cybmeta
-
2 réponses
- votes
-
- 2016-04-15
Mise àjour: d'après ce quej'ai lu,vous devezfaireplusieursbouclespour obtenir le access_token,que vous utilisezensuitepourfaire la requête
- Acquisition d'informations d'identificationtemporaires: le client obtient unensemble d'informations d'identificationtemporaires du serveur.
- Autorisation: l'utilisateur "autorise" lejeton de demande à accéder à son compte.
- Échange dejetons: le client échange lesinformations d'identificationtemporaires de courte durée contre unjeton de longue durée.
Update: From what I've read, you need to do multiple curls to get the access_token, which you then use to do the query
- Temporary Credentials Acquisition: The client gets a set of temporary credentials from the server.
- Authorization: The user "authorizes" the request token to access their account.
- Token Exchange: The client exchanges the short-lived temporary credentials for a long-lived token.
-
- 2016-09-20
Je sais quej'arrive unpeutard,maispouvez-vous utiliser wp_remote_getet _post?
Jetireet publie du contenu avecmoninstallation wordpressen les utilisant:
Telleest l'idéegénérale du codex wordpress:
$response = wp_remote_post( $url, array( 'body' => $data, 'httpversion' => '1.0', 'sslverify' => false, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ), ), ) );
Voici unexempleplus spécifique:
$url='http://WWW.EXAMPLE HERE.'; $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', //needed to get a response 'blocking' => true, 'headers' => array('Authorization' => 'Basic ' . base64_encode( 'MY TOKENID' . ':' . '' )), 'body' => $body // in array 'cookies' => array() ) ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { // echo 'Response:<pre>'; // print_r( $response ); // echo '</pre>'; $responseBody = json_decode($response['body'],true); echo $responseBody['message']; } } }
L'astuce consiste àencoder lenom d'utilisateuret pw. Maintenant,souvent,letempsen fonction dunom d'utilisateur de l'APIet pw sera soit vide,soit vosjetons.
parexemple,dansmonexemple spécifique ci-dessus,lesen-têtes étaient
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'MYTOKENID' . ':' . '' ))
et j'ai laissépw vide. Cela dépend du système d'API que vous utilisez.
I know I'm coming into this a bit late, but can you use wp_remote_get and _post?
I'm pulling and posting content with my wordpress install using them:
THis is the general idea from the wordpress codex:
$response = wp_remote_post( $url, array( 'body' => $data, 'httpversion' => '1.0', 'sslverify' => false, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ), ), ) );
Here is a more specific example:
$url='http://WWW.EXAMPLE HERE.'; $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', //needed to get a response 'blocking' => true, 'headers' => array('Authorization' => 'Basic ' . base64_encode( 'MY TOKENID' . ':' . '' )), 'body' => $body // in array 'cookies' => array() ) ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { // echo 'Response:<pre>'; // print_r( $response ); // echo '</pre>'; $responseBody = json_decode($response['body'],true); echo $responseBody['message']; } } }
The trick is encoding the username and pw. Now often time depending on the API username and pw will either be blank or will be your tokens.
so for instance in my specific example above, the headers were
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'MYTOKENID' . ':' . '' ))
and i left pw blank. That's up to the API system you're using though.
J'essaie d'utiliser WordPress Rest Api avec authentificationpour obtenirplus de données de l'API. J'aiinstallé leplugin Oauth,leplugin rest-apiet obtenu lesinformations d'identification d'API de WP-CLI.
J'aitrouvé comment accéder aux données sans autorisation. Celafonctionne:
Maisje ne saispas commentm'authentifier avec desinformations d'identification. Voicimatentative. Jene saispas si "clé"et "secret" sont corrects.
La sortieest
Commentpuis-jefairefonctionner cela? Merci.