title: Bad respond when trying to get authentication token for Reddit api 'Application Only OAuth'
body: <p>So i been trying to get access to the reddit api.</p>
  <p>I registered to reddit. verified my mail. opened an app and got my credentials.</p>
  <p><a href="https://github.com/reddit-archive/reddit/wiki/OAuth2#application-only-oauth" rel="nofollow noreferrer">Followed this official documentation</a> and also came across to <a href="https://ranjithnair.github.io/2018/02/27/Reddit-Application-Oauth.html" rel="nofollow noreferrer">this tutorial</a></p>
  <p>All my efforts have failed and don't get any respond.</p>
  <p>I am using nodejs. but also tried in postman and failed.</p>
  <p>Tried 2 options using fetch and using axios:</p>
  <pre><code>
      const axios = require('axios');
  const fetch = require('node-fetch')
  const { URLSearchParams } = require('url')
  
  
  
  class RedditApi {
  
      clientId2 = &quot;get ur own credentials by opening an app here https://www.reddit.com/prefs/apps&quot;;
      clientSecret2 = &quot;get ur own credentials by opening an app here https://www.reddit.com/prefs/apps&quot;;
  
      authenticationUrl = `https://www.reddit.com/api/v1/access_token`;
  
      BASE_URL = 'https://www.reddit.com/';
  
      tokenAuth = null;
      tokenExpirationTime = null;
  
      currencyObj = null;
  
      constructor(currencyObj) {
          this.currencyObj = currencyObj;
          console.log(&quot;constructor service&quot;)
      }
  
      async getAuthToken() {
  
          const bodyParams = new URLSearchParams({
              grant_type: &quot;https://oauth.reddit.com/grants/installed_client&quot;,
              device_id: &quot;DO_NOT_TRACK_THIS_DEVICE&quot;
          });
  
          console.log(this.clientId2, 'this.clientId');
          debugger;
  
          const headersObj = {
              'Authorization': `Basic ${Buffer.from(`${this.clientId2}:`).toString('base64')}`,
              'Content-Type': 'application/x-www-form-urlencoded',
          };
  
          let response = null;
          try {
              response = await axios.post(this.authenticationUrl,
                  bodyParams,
                  {
                      headers: headersObj
                  });
              debugger;
          } catch (error) {
              debugger;
              console.error(error);
              console.log(error.stack);
              return null;
          }
      }
  
  
      async getAuthToken2() {
          try {
              // Creating Body for the POST request which are URL encoded
              const params = new URLSearchParams()
              params.append('grant_type', 'https://www.reddit.com/api/v1/access_token')
              params.append('device_id', 'DO_NOT_TRACK_THIS_DEVICE')
  
              // Trigger POST to get the access token
              const tokenData = await fetch('https://oauth.reddit.com/grants/installed_client', {
                  method: 'POST',
                  body: params,
                  headers: {
                      'Content-Type': 'application/x-www-form-urlencoded',
                      'Authorization': `Basic ${Buffer.from(`${this.clientId2}:`).toString('base64')}` // Put password as empty
                  }
              }).then(res =&gt; {
                  debugger;
  
                  return res.text()
              })
              debugger;
  
              if (!tokenData.error) {
                  debugger;
  
                  res.send(trendingResult)
              }
              res.status(tokenData.error).send(tokenData.message)
          } catch (error) {
              debugger;
              console.log(error)
          }
      }
  }
  
  module.exports = RedditApi;
  </code></pre>
  <p>when using axios i get this respond: &quot;Request failed with status code 401&quot;</p>
  <p>When using fetch i get this respond:  &quot;'<h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n\n'&quot;</p>
  <p>Anybody knows what is the problem and how can i fix it?
  Many thanks!</p>