# Autentification

{% hint style="info" %}
Avant de pouvoir envoyer des requêtes authentifiées, il vous faudra les[accreditation](https://doc.demarches-simplifiees.fr/api-graphql/accreditation "mention")s nécessaire. Aussi pensez a consulter notre doc sur l'[jeton-dauthentification](https://doc.demarches-simplifiees.fr/api-graphql/jeton-dauthentification "mention").
{% endhint %}

Vous pouvez tester en executant le script suivant avec les variables d'environnement adaptées :

<pre class="language-bash"><code class="lang-bash"><strong>API_TOKEN="votre_token" DEMARCHE_NUMBER=votre_numero_de_demarche ruby auth.rb
</strong></code></pre>

{% code title="auth.rb" %}

```ruby
require 'net/http'
require 'uri'
require 'json'
ENDPOINT = URI('https://demarche.numerique.gouv.fr/api/v2/graphql')

### that's the GraphQL part.
def query
  <<~GRAPHQL_QUERY
query getDemarche($demarcheNumber: Int!) {
  demarche(number: $demarcheNumber) {
    id
    dossiers {
      nodes {
        id
        demandeur {
          ... on PersonnePhysique {
            civilite
            nom
            prenom
          }
          ... on PersonneMorale {
            siret
          }
        }
      }
    }
  }
}
GRAPHQL_QUERY
end


### that's the HTTP part
# open an http connexion to our GraphQL endpoint
def open_http_connection
  http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
  if ENDPOINT.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end

# the headers of our http query, include auth
def request_headers
  {
    "Content-Type" => "application/json",
    "Authorization" => "Bearer #{ENV['API_TOKEN']}"
  }
end

# given an http connexion, request the API
def request_page(http)
  # the data of our query
  data = {
    "query" => query,
    "operationName" => "getDemarche",
    "variables" => {
      "demarcheNumber": ENV['DEMARCHE_NUMBER'].to_i
    }
  }
  
  # the HTTP format
  req = Net::HTTP::Post.new(ENDPOINT, request_headers)
  # the request body format
  req.body = data.to_json

  response = http.request(req)

  data = JSON.parse(response.body)
  data
end

http = open_http_connection
data = request_page(http)
puts "Info: fetched it should work: #{data}"

```

{% endcode %}
