Doug C.
Doug C.

Reputation: 61

Oauth2 for Quickbooks Online - Getting Invalid Client in VBA when trying to exchange Authorization Code for Token

I have a customer who recently upgraded to Quickbooks Online. In the past they used a MS Access database to interface Quickbooks with other data sources. Since they still want to utilize the Access database with Quickbooks and I need to run through the oAuth2 process. I have been able to get the first step done (handshake/authorization) and get code. I am stuck when it comes to exchanging that for a token. I am using the following VBA Code:

Dim httpReq As New MSXML2.ServerXMLHTTP60 'MSXML2.ServerXMLHTTP
Dim sBase64 as string
Dim sBody as string
Dim sKey as string

sBase64 = "Basic " & EncodeBase64 <myclient_idXXX:myclient_secretXXX>
sKey = <Authorization Code from step 1 of oAuth process>

httpReq.Open "POST", "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer? HTTP/1.1"

httpReq.setRequestHeader "Accept", "application/json"
httpReq.setRequestHeader "Authorization", sBase64
httpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
httpReq.setRequestHeader "Host", "oauth.platform.intuit.com"

sBody = "grant_type=authorization_code&code=" & skey & "&redirect_uri=http://localhost"

httpReq.send sBody

The error I get is a 401 and it description is {"error":"invalid_client"}

I have been working on this for days, there is very little documentation for using VBA on oAuth2 process and none with Quickbooks, but I have to believe someone, somewhere has to have done this with Quickbooks before and that the above code is close to working.

I have been using this document from intuit: https://developer.intuit.com/docs/00_quickbooks_online/2_build/10_authentication_and_authorization/10_oauth_2.0#/Sample_code

The above code is translated into VBA from this:

POST https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer HTTP/1.1
Accept: application/json
Authorization: Basic UTM0dVBvRDIwanp2OUdxNXE1dmlMemppcTlwM1d2
NzRUdDNReGkwZVNTTDhFRWwxb0g6VEh0WEJlR3dheEtZSlVNaFhzeGxma1l
XaFg3ZlFlRzFtN2szTFRwbw==
Content-Type: application/x-www-form-urlencoded
Host: oauth.platform.intuit.com
Body: grant_type=authorization_code&
code=L3114709614564VSU8JSEiPkXx1xhV8D9mv4xbv6sZJycibMUI&
redirect_uri=https://www.mydemoapp.com/oauth-redirect

Thanks in advance!

Upvotes: 1

Views: 1164

Answers (1)

user10905687
user10905687

Reputation: 11

I had the same issue. The authorization code was incorrect. If you try to debug.print the encoded authorization you will find a carriage return. I fixed it by removing it.

AuthorizationHeader = 
    Replace(Base64Encode(client_id & ":" & client_secret), Chr(10), "")

Upvotes: 1

Related Questions