Search code examples
roauthoauth-2.0exact-online

Connecting R Studio to Exact Online API


I am trying to establish a connection with the Exact Online API and am following the steps listed at the Exact Online community page: https://support.exactonline.com/community/s/knowledge-base#All-All-DNO-Task-oauth-eol-oauth-dev-oauth2tut.

I managed to complete Step 1 and Step 2. That is, I am receiving the authorization code like listed in the example: "Actual response: https://www.mycompany.com/myapplication?code=XTzM!IAAAACbPTzQJXwFhM..."

I have to use this code in Step 3, but I can not seem to get it working. I use all the listed parameters but the response I get is:

Response [https://start.exactonline.nl/api/oauth2/token]
Date: 2018-04-14 10:58
Status: 400
Content-Type: text/html
Size: 11 B

I have no idea what I am doing wrong. The code I use for Step 3 is:

url <- "https://start.exactonline.nl/api/oauth2/token"
POST(url, add_headers("Content-type" = "application/x-www-form-urlencoded"),
    body = list(code="[CODE_FROM_STEP_2]",
        redirect_uri="[MY_WEBSITE_URI]", client_id="[MY_CLIENT_ID]", 
        client_secret="[MY_CLIENT_SECRET]", grant_type="authorization_code"))

If anyone can help me out with this I will be very grateful! Thanks.

EDIT: using verbose() in the POST call, the Status: 400 error is stated as HTTP/1.1 400 Bad Request.


Solution

  • For anyone interested, I resolved this problem by using Postman to obtain the first access and refresh tokens. I then use the refresh token to obtain new access and refresh tokens using the following code:

    a <- POST(url,
          body = list(refresh_token = {refresh_token},
                      grant_type = "refresh_token", client_id = {client_id}, 
                      client_secret = {client_secret}),
          encode = "form")
    a.df <- as.data.frame(fromJSON(content(a,type="text")))
    

    This will get you an access token, token type, expiring time and refresh token in a dataframe.