title: Datasource Authentication via Routes for Grafana Datasource Plugin
body: <p>I'm trying to write a custom data source plugin for <a href="https://github.com/grafana/grafana/" rel="nofollow noreferrer">Grafana</a> that would request Azure AD authentication tokens and send them along with the queries to my database which will accept the token and return the response to the query.</p>
  
  <p>I've noticed that the Azure Monitor Plugin for Grafana does the same by asking the user to enter their client id, client secret and tenant id and using it via the routes{} part of their <a href="https://github.com/grafana/azure-monitor-datasource/blob/master/src/plugin.json" rel="nofollow noreferrer">plugin.json</a> file.</p>
  
  <p>I have followed this method but I get an error:</p>
  
  <blockquote>
    <p>502 bad gateway error.</p>
  </blockquote>
  
  <p>My files are hosted <a href="https://github.com/abhinavreddy98/kustodb-for-grafana/tree/master/src" rel="nofollow noreferrer">here</a></p>
  
  <p>The essential part of my datasource.js that makes the HTTP call is</p>
  
  <pre><code>query(options) {
  
      const csl = document.getElementById("csl").value;
      var queries = _.filter(options.targets, item =&gt; {
          return item.hide !== true;
        }).map(item =&gt; {
          return {
            refId: item.refId,
            intervalMs: options.intervalMs,
            maxDataPoints: options.maxDataPoints,
            format: item.format,
          };
        });
      if (queries.length &lt;= 0) {
        return this.$q.when({data: []});
      }
      return this.backendSrv.datasourceRequest({
          url: `api/datasources/proxy/${this.id}/kusto/query`,
          method: 'POST',
          headers: this.headers,
          data: {
              db: this.database,
              csl: csl,
              from: options.range.from,
              to: options.range.to,
              queries: queries,
          }
      });
  }
  </code></pre>
  
  <p>Where kusto is the routes path defined in my plugin.json.</p>
  
  <p>What is causing this error? Is there a mistake in my datasource.js or my plugin.json? Is the error happening client side or server side?</p>