Come utilizzare l'autenticazione OAuth con l'API REST tramite i comandi CURL?
-
-
Le cosenon sono cosìfacili.Hoprovato a scrivere una rispostama èpiuttosto lunga.Potrestiiniziare leggendoi documenti,specialmente [The Authorization Flow] (https://github.com/WP-API/OAuth1/blob/master/docs/basics/Auth-Flow.md).Questopost ha anche un [ottimotutorial] (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 risposta
- voti
-
- 2016-04-15
Aggiornamento: da quello che ho letto,devieseguirepiù riccioliper ottenere access_token,chepoi utilizzipereseguire la query
- Acquisizione di credenzialitemporanee:il client ottiene una serie di credenzialitemporanee dal server.
- Autorizzazione: l'utente "autorizza"iltoken di richiesta ad accedere alproprio account.
- Token Exchange:il client scambia le credenzialitemporanee dibreve durata con untoken di lunga durata.
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
So che ci sto arrivando unpo 'tardi,mapuoi usare wp_remote_gete _post?
Stoestraendoe pubblicando contenuti con lamiainstallazione di wordpress che li utilizza:
Questa è l'ideagenerale dal codice wordpress:
$response = wp_remote_post( $url, array( 'body' => $data, 'httpversion' => '1.0', 'sslverify' => false, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ), ), ) );
Ecco unesempiopiù specifico:
$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']; } } }
Iltrucco è codificareilnome utentee pw. Ora spessoiltempo a seconda delnome utente dell'APIe pw sarà vuoto o saràiltuotoken.
quindi,adesempio,nelmioesempio specifico sopra,leintestazionierano
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'MYTOKENID' . ':' . '' ))
e ho lasciatopw vuoto. Dipende dal sistema API che stai utilizzando.
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.
Sto cercando di utilizzare WordPress Rest Api con l'autenticazioneper ottenerepiù dati dall'API. Hoinstallatoilplug-in Oauth,ilplug-in rest-apie ho ottenuto le credenziali API da WP-CLI.
Ho capito come accedere ai dati senza autorizzazione. Funziona:
Manon riesco a capire come autenticarmi con le credenziali. Eccoilmiotentativo. Non sono sicuro che "chiave"e "segreto" siano corretti.
L'output è
Comepossofarlofunzionare? Grazie.