/*jshint esversion: 6 */
/*jshint node: true */

"use strict";

var request = require('request');
var OAuth = require('oauth');

var API_ROOT =  'https://disqus.com/api/3.0/%s.json';
var AUTHORIZE_URL = 'https://disqus.com/api/oauth/2.0/authorize';
var ACCESS_TOKEN_URL = 'https://disqus.com/api/oauth/2.0/access_token/';

var resourcesRequiringPost = ['blacklists/add', 'blacklists/remove', 'categories/create', 'exports/exportForum', 'forums/addModerator', 'forums/create', 'forums/removeModerator', 'posts/approve', 'posts/create', 'posts/highlight', 'posts/remove', 'posts/report', 'posts/restore', 'posts/spam', 'posts/unhighlight', 'posts/update', 'posts/vote', 'reactions/remove', 'reactions/restore', 'threads/close', 'threads/create', 'threads/open', 'threads/remove', 'threads/restore', 'threads/subscribe', 'threads/unsubscribe', 'threads/update', 'threads/vote', 'users/checkUsername', 'users/follow', 'users/unfollow', 'whitelists/add', 'whitelists/remove'];

// *********************************************************************** //
class Disqus {
  constructor(conf) {
    this.conf = conf;
    this.oauth2 = new OAuth.OAuth2(
      this.conf.api_key,
      this.conf.api_secret,
      '',
      AUTHORIZE_URL,
      ACCESS_TOKEN_URL
    );
    this.oauth2.useAuthorizationHeaderforGET(true);
  }
  
// *********************************************************************** //
  getHttpVerb(resource) {
    return resourcesRequiringPost.indexOf(resource) !== -1 ? 'post' : 'get';
  }
  
// *********************************************************************** //
  request(resource, qs, callback) {
    var method = this.getHttpVerb(resource);
    var url = API_ROOT.replace('%s', resource);
    Object.assign(qs, this.conf);
    
    return request[method]({ url, qs }, (e, res, body) => {
      callback(res.statusCode === 200 ? body : { error: { body, e, res } });
    });
  }

// *********************************************************************** //
  getAuthorizeUrl(type, uri, scope, state) {
    var url = this.oauth2.getAuthorizeUrl(
        {response_type: type,
         redirect_uri: uri,
         scope: scope,
         state: state
        });
    // There is a bug somewhere in either the Disqus OAuth code or the OAuth NPM
    // that prepends "NULL" to the front of the URL. So we strip it off before returning it
    var pos = url.indexOf("https:");
    url = url.slice(pos);
    return url;
  }

// *********************************************************************** //
  getOAuthAccessToken(code, uri, grantType, callback) {
    var self = this;
    this.oauth2.getOAuthAccessToken(code, {
          client_id: self.conf.api_key,
          client_secret: self.conf.api_secret,
          redirect_uri: uri,
          grant_type: grantType
    }, (err, access_token, refresh_token, results) => {
        if (err) {
            return callback(err);
        } else {
            self.conf.access_token = access_token;
            return callback(null, access_token, refresh_token, results);
        }
    });
  }
}

module.exports = Disqus;

@@ -2548,19 +2548,8 @@
 uri,
- grantType,
  cal
@@ -2764,17 +2764,28 @@
 pe: 
-grantType
+'authorization_code'
 %0A   
@@ -2843,18 +2843,16 @@
 %7B%0A      
-  
 if (err)
@@ -2866,48 +2866,384 @@
     
-    return callback(err);%0A        %7D else
+return callback(err);%0A      %7D else %7B%0A        self.conf.access_token = access_token;%0A        return callback(null, access_token, refresh_token, results);%0A      %7D%0A    %7D);%0A  %7D%0A  %0A// *********************************************************************** //%0A  refreshOAuthAccessToken(refreshtoken, callback) %7B%0A    var self = this;%0A    this.oauth2.getOAuthAccessToken(refreshtoken,
  %7B%0A 
@@ -3247,25 +3247,34 @@
 %7B%0A          
- 
+client_id:
  self.conf.a
@@ -3265,32 +3265,262 @@
 _id: self.conf.a
+pi_key,%0A          client_secret: self.conf.api_secret,%0A          grant_type: 'refresh_token'%0A    %7D, (err, access_token, refresh_token, results) =%3E %7B%0A      if (err) %7B%0A        return callback(err);%0A      %7D else %7B%0A        self.conf.a
 ccess_token = ac
@@ -3527,28 +3527,24 @@
 cess_token;%0A
-    
         retu
@@ -3602,26 +3602,24 @@
 lts);%0A      
-  
 %7D%0A    %7D);%0A  
@@ -3611,24 +3611,25 @@
    %7D%0A    %7D);
+ 
 %0A  %7D%0A%7D%0A%0Amodu

Add refreshOAuthAccessToken method Remove grant_type from getOAuthAccessToken