hjalali
(Hossein Jalali)
31 Maggio 2020, 10:31pm
1
Ho una funzione molto semplice che “dovrebbe” funzionare, ma per qualche motivo non lo fa. Qualcuno può aiutarmi a capire cosa stiamo sbagliando?
Continuiamo a ricevere l’errore “BAD CSRF”.
public function changeName()
{
$url = 'https://www.website.com/{username}.json';
$data = ['name' => 'James', 'api_key' => DISCOURSE_API, 'api_username' => 'system'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
echo $response = curl_exec($ch);
if (!$response)
{
return false;
}
}
A proposito, ho già provato a spostare api_key e api_username nell’URL sopra come GET, ma non cambia nulla.
1 Mi Piace
simon
31 Maggio 2020, 11:49pm
2
You need to put the Api-Key and Api-Username values in the request header. There’s a curl example near the end of this topic that could be helpful: Sync SSO user data with the sync_sso route .
5 Mi Piace
hjalali
(Hossein Jalali)
1 Giugno 2020, 12:16am
3
Thanks a lot! That did the trick.
For anyone else with the same problem:
$url = 'https://www.website.com/{username}.json';
$data = ['name'=>'George'];
$api_key = CUSTOM_DISCOURSE_API;
$headers = array("Content-Type: multipart/form-data;","Api-Key: $api_key","Api-Username: system",);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$result = curl_exec( $ch );
if ( curl_errno( $ch ) !== 0 ) {
// Handle error, call curl_close( $ch ) and return.
}
curl_close( $ch );
$discourse_user = json_decode( $result );
4 Mi Piace
hjalali
(Hossein Jalali)
1 Giugno 2020, 12:29am
4
Just to add to that, if someone needs to update a “user_field” replace the $data with this:
$data = ['user_fields' => ['1' => 'Something']];
The number “1” being the first user_field I created (as they are assigned by number not name).
2 Mi Piace
brospars
(Benoit Rospars)
5 Ottobre 2021, 9:32am
5
Da quando è obbligatorio?
Non l’ho trovato nel changelog. Ho creato uno strumento 2-3 anni fa per creare categorie in batch che funzionava perfettamente e ora ricevo questo errore…
blake
(Blake Erickson)
5 Ottobre 2021, 3:39pm
6
blake:
Avviso di deprecazione!
Il 6 aprile 2020 abbiamo eliminato il supporto per tutte le autenticazioni non basate su intestazioni HTTP (esclusi alcuni percorsi rss, ricezione email e ics). Ciò significa che le richieste API con api_key e api_username nei parametri della query o nel corpo HTTP della richiesta smetteranno presto di funzionare.
Ci scusiamo per eventuali problemi causati; abbiamo implementato questa modifica in modo graduale e abbiamo avvisato gli utenti nel modo migliore possibile, ma è difficile intercettare ogni caso di deprecazione.
2 Mi Piace