Quickbook/Intuit API - Refresh token (Oauth2)

1.1k views Asked by At

I'm trying to generate an access token from a refresh token on the OAuth2 of Intuit API (in order to access to the Quickbook API). I achieve to do it in Node.js with the library axios on my laptop, but axios is not available on my server, where my script will be executed. And i doesn't achieve to do it with another library (https, xmlhttprequest, oauth, node-fetch), it'a a non-sense ! Requests return me error 400 Invalid request, so it doesn't help a lot.


Here is the script on axios, which works :

async function quickbookTokenDirect(){
    let url = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'
    let header = {
        Authorization:'Basic <Authorization code found on https://developer.intuit.com/app/developer/playground>',
        Accept:'application/json',
        'Content-Type':'application/x-www-form-urlencoded',
    }
    let body={
            grant_type : 'refresh_token',
            refresh_token : '<Refresh token found on https://developer.intuit.com/app/developer/playground>',
    }
    let result = await axios({method:'post', url:url, headers:header, data:body})
    .then( response=>{console.log(response)})
    .catch( response=>{console.log(response)})
}
There is the script on https, that doesn't works and i doesn't see why :


async function quickbookTokenHttps(){
    const https = require('https')
    //let url = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'
    let header = {
        Authorization:'Basic <Authorization code found on https://developer.intuit.com/app/developer/playground>',
        Accept:'application/json',
        'Content-Type':'application/x-www-form-urlencoded',
    }
    let body={
            grant_type : 'refresh_token',
            refresh_token : '<Refresh token found on https://developer.intuit.com/app/developer/playground>',
    }
    let options = {
        hostname:'oauth.platform.intuit.com',
        path:'/oauth2/v1/tokens/bearer',
        method:'post',
        headers:header,
        data:body,
        //body:body
    }
    let req = await https.request(options, (res) => {
        let result=''
        console.log(res.statusCode)
        console.log(res.statusMessage)
        res.on('data', (chunk) => result +=chunk)
        res.on('end', () => {console.log(JSON.parse(result))})
    })
    .on('error', (err) => {console.log(err)})
    .end()
}

If some of you have an idea, it will help me a lot !
1

There are 1 answers

12
Bench Vue On

I can get the refresh token using Intuit node.js example.

https://github.com/IntuitDeveloper/oauth2-nodejs

The https.request header's filed needs a single quotes And Authorization Basic needs to combine clientID and ClientSecret

From

    let header = {
        Authorization:'Basic <Authorization code found on https://developer.intuit.com/app/developer/playground>',
        Accept:'application/json',
        'Content-Type':'application/x-www-form-urlencoded',
    }

To

    let header = {
        'Authorization':'Basic btoa(toString(<clientID>) + ':' + toString(<clientSecret>))',
        'Accept':'application/json',
        'Content-Type':'application/x-www-form-urlencoded',
    }

Get well-known URLs

https://developer.api.intuit.com/.well-known/openid_sandbox_configuration

Response

{
  "issuer": "https://oauth.platform.intuit.com/op/v1",
  "authorization_endpoint": "https://appcenter.intuit.com/connect/oauth2",
  "token_endpoint": "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer",
  "userinfo_endpoint": "https://sandbox-accounts.platform.intuit.com/v1/openid_connect/userinfo",
  "revocation_endpoint": "https://developer.api.intuit.com/v2/oauth2/tokens/revoke",
  "jwks_uri": "https://oauth.platform.intuit.com/op/v1/jwks",
 ... removed

Add redirect URL in Intuit.com of developer portal in your project at dashboard.

enter image description here

http://localhost:3000/callback

enter image description here

config.json at oauth2-nodejs directory

  "clientId": "your client id",
  "clientSecret": "your client secret",
  "redirectUri": "http://localhost:3000/callback",
  "configurationEndpoint": "https://developer.api.intuit.com/.well-known/openid_sandbox_configuration/",
  "api_uri": "https://sandbox-quickbooks.api.intuit.com/v3/company/",

At the same directory

npm install
node app.js

Debugging Refresh code in VS code refresh token

enter image description here

refresh token in middleware(client-oauth2) enter image description here

Browser login

http://localhost:3000/

enter image description here

Just login before refresh token

enter image description here

After refresh token

enter image description here

Again the basic Auth code and this line

  this.basicAuth = require('btoa')(authConfig.clientId + ':' + authConfig.clientSecret)
    request({
      url: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer',
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Authorization': 'Basic ' + tools.basicAuth,
        'Content-Type' : 'application/x-www-form-urlencoded'
      },
      form: {
        refresh_token: token,
        grant_type: 'refresh_token'
      }