title: How to I make a nonce or realm request for a digest authentication?
body: <p>For my project I want to connect to the provided <a href="https://developers.shopware.com/developers-guide/rest-api/" rel="nofollow noreferrer">Shopware API</a> Since I'm not that experienced with REST Services I cloned an existing HTTPClient (using the Apache Components) from <a href="https://github.com/sveneusewig/ShopwareJavaApiClient/blob/master/src/RestApiHttpClient.java" rel="nofollow noreferrer">Github</a>. I have to add that Shopware offers the DigestAuth as Authentication Method.</p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>public RestApiHttpClient(URL apiEndpoint, String username, String password) {
      this.apiEndpoint = apiEndpoint;
  
      BasicHttpContext context = new BasicHttpContext();
      this.localContext = HttpClientContext.adapt(context);
      HttpHost target = new HttpHost(this.apiEndpoint.getHost(), -1, this.apiEndpoint.getProtocol());
      this.localContext.setTargetHost(target);
  
  
      TargetAuthenticationStrategy authStrat = new TargetAuthenticationStrategy();
      UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
      BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
      AuthScope aScope = new AuthScope(target.getHostName(), target.getPort());
      credsProvider.setCredentials(aScope, creds);
  
      BasicAuthCache authCache = new BasicAuthCache();+
  
      // Digest Authentication
      DigestScheme digestAuth = new DigestScheme(Charset.forName("UTF-8"));
      authCache.put(target, digestAuth);
      this.localContext.setAuthCache(authCache);
      this.localContext.setCredentialsProvider(credsProvider);
  
  
      ArrayList&lt;Header&gt; defHeaders = new ArrayList&lt;&gt;();
      defHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()));
      this.httpclient = HttpClients.custom()
              .useSystemProperties()
              .setTargetAuthenticationStrategy(authStrat)
              .disableRedirectHandling()
              .disableContentCompression()
              .setDefaultHeaders(defHeaders)
              .setDefaultCredentialsProvider(credsProvider)
              .build();
      }
  </code></pre>
  
  <p>So if i test this script i get this error:</p>
  
  <pre><code>Dez 07, 2018 9:00:55 AM org.apache.http.impl.auth.HttpAuthenticator generateAuthResponse
  SCHWERWIEGEND: DIGEST [complete=false, nonce=null, nc=0] authentication error: missing realm in challenge
  {"success":false,"message":"Invalid or missing auth"}
  </code></pre>
  
  <p>I tried overwriting the paramters (first only the realm, than realm and nonce, than every parameter) but with no success:</p>
  
  <pre><code>digestAuth.overrideParamter("realm", "Shopware REST-API");
  digestAuth.overrideParamter("nonce", "somerandomnumber");
  // or create random number;
  digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
  digestAuth.overrideParamter("qop", "auth");
  digestAuth.overrideParamter("nc", "0");
  digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());
  </code></pre>
  
  <p>Im guessing the DigestAuth need's the nonce value or the whole header from the server, but I couldn't find a method to store these values. Also tried <a href="https://stackoverflow.com/a/8572867/9805615">this</a> one with no success.</p>
  
  <p>Any ideas? thank you in advance!</p>