Impossibile aggiornare l'email tramite API: errore invalid_access

Hi Guys,

Im getting an error when trying to use the API to update email.
I have looked at the logs and can see the following:

Started PUT “/discussion/users/davetest/preferences/email.json” for xx.xx.xx.xx at 2018-05-11 12:22:11 +0000

Processing by UsersEmailController#update as JSON

Parameters: {“api_key”=>;"[FILTERED]", “api_username”=>;“daveAdmin”, “email”=>;“[new.email@example.com](mailto:new.email@example.com)”, “username”=>;“davetest”}

Can’t verify CSRF token authenticity.

This is a PUT request, but looking at the scopes below, doesnt seem like PUT is supported for the WRITE scope? surely im misunderstanding, so could someone please assist?

I have re-generated the all-user api-key, also created specific user api-keys to use, but same result.

SCOPES = {
read: [:get],
write: [:get, :post, :patch],
message_bus: [[:post, ‘message_bus’]],
push: nil,
notifications: [[:post, ‘message_bus’], [:get, ‘notifications#index’], [:put, ‘notifications#mark_read’]],
session_info: [[:get, ‘session#current’], [:get, ‘users#topic_tracking_state’]]
}

Can you verify the content-type that you are making the API request as? It needs to be

"Content-Type: multipart/form-data;"

If it is not that content type you may see the CSRF token error.

Hi, thanks for the reply.
I actually had tried it both with ‘application/json’ and ‘application/x-www-form-urlencoded’ and neither worked. I have now tried it with what you suggested (via postman) and getting:

“You are not permitted to view the requested resource. The API username or key is invalid.”

I have regenerated the global api key that is used for “all users” and using it with my own username, which is an admin too…

shahid

Here is an example of my postman request maybe you can spot any differences with what you are doing? I’m also using the “all users” key with an admin username.

yes, its the same, im using the Postman API collection supplied by discourse. So using it as suggested. I will try again tonight and see if i get any different results. Will update when ive tried. Thanks for assisting, much appreciated. Do let me know if you have any other idea.

Anche io sto riscontrando questo problema e mi chiedo se sia correlato all’uso di un provider OAuth per l’accesso, bloccando così questa pagina specifica?

È molto probabile che non sia il provider OAuth, poiché le chiavi API lo aggirano.

Potresti condividere il codice della tua richiesta API?

in python:

import json
import requests
import time

my_token = ''

url_base = '<>'
update_email = url_base + "users/<my_username>/preferences/email.json"

headers = {
    "Content-Type": "multipart/form-data",
    'user-agent': 'my-app/0.0.1',
    'Api-Key': my_token,
    'Api-Username': '<my_username>'
}

data = {
  "email": "<new_email>"
}

response = requests.put(update_email, data=data, headers=headers)

print(response.text)

Modifica: un codice molto simile per impostare i gruppi degli utenti, ecc., ha funzionato, quindi la combinazione chiave/nome utente (che è un utente con privilegi di amministratore) funziona.

Modifica 2: Da quanto ho dedotto navigando nel forum, questo deve essere fatto direttamente nel database (o meglio, tramite la console), il che è impossibile per noi.

Hai altre riflessioni al riguardo? Il mio parere è che una persona con diritti di amministratore dovrebbe poter impostare l’e-mail di un utente.

In che senso? Non credo sia possibile con l’interfaccia web esistente, che definisce l’API. Quindi non può essere fatto.

Intendi dire che la UX/API dovrebbe permettere di impostare l’indirizzo email di un utente su quello che si desidera. (Non che tu creda che sia attualmente possibile.)

Puoi avviare una modifica (tramite UX e presumibilmente API) modificando il loro profilo e inserendo il nuovo indirizzo, ma non verrà aggiornato finché non cliccheranno sul link nella loro email. È sufficiente? Se no, dovrai farlo dalla console o tramite un plugin.

Esatto.

Ogni volta che inserisco qualcosa nell’interfaccia utente per modificare l’email (a cui posso accedere solo tramite l’URL diretto, ovvero https://<discourse>/u/<username>/preferences/email, poiché non c’è alcun link nel profilo dell’utente), appare un messaggio che dice che non posso farlo dopo aver cliccato su “Cambia”:

E questo succede sul mio stesso account da amministratore. Non ricevo nessuna email.

Anche io sono arrivato a questo punto…

@pfaffman: hai affermato che è possibile modificare l’indirizzo email nel profilo, ma ciò richiede che sia impostato email editable, vero?

Ho appreso che non esiste alcun modo per un amministratore o per una chiamata API amministrativa di avviare la modifica dell’indirizzo email a meno che non sia consentito anche agli utenti di modificarlo, corretto? Posso tranquillamente accettare che venga inviata un’email di conferma e che la modifica debba essere autorizzata dall’utente.

Il mio caso d’uso è che gli utenti verranno gestiti tramite un’altra applicazione attraverso l’API. Quindi, non voglio che possano modificare i propri indirizzi email da soli, solo su Discourse.

Potreste implementare l’Single Sign On e rendere la vostra altra applicazione la fonte principale per gli accessi?

Esiste una specifica impostazione SSO chiamata sso overrides email che fa esattamente ciò che state cercando di ottenere. Se avete configurato l’SSO, potete quindi utilizzare l’endpoint sync_sso e passare il nuovo indirizzo email.

Grazie @blake, è una sfida divertente :wink:

Dovrei importare le password da Discourse all’hub identità… oh mio.

Ho aiutato il mio scenario originale con un processo in tre passaggi: abilita la modifica dell’email, avvia la modifica dell’indirizzo email, disabilita la modifica dell’email. Sembra un po’ eccessivo, ma funziona.

Ci penserò per lo scenario SSO.