VBA-tools / VBA-Web

VBA-Web: Connect VBA, Excel, Access, and Office for Windows and Mac to web services and the web
http://vba-tools.github.io/VBA-Web/
MIT License
2.01k stars 494 forks source link

Sage Online Accounting API - "The auth code you transmitted has an unexpected format" #430

Closed DCNetSolutions closed 4 years ago

DCNetSolutions commented 4 years ago

Hello All,

I have been tasked with connecting our Access program with Sage Online Accounting software. I am struggling with getting the token. I am using the following code but receiving back the message: "The auth code you transmitted has an unexpected format". The code I am using:

On Error GoTo auth_Cleanup

    Dim auth_TokenClient As WebClient
    Dim auth_Request As New WebRequest
    Dim auth_Response As WebResponse
    Dim str_AuthorizationCode As String

        ' Clone client (to avoid accidental interactions)
    Set auth_TokenClient = New WebClient
    Set auth_TokenClient.Authenticator = Nothing
    auth_TokenClient.BaseUrl = "https://oauth.accounting.sage.com/token"

    ' Prepare token request
    auth_Request.Method = WebMethod.HttpPost
    auth_Request.RequestFormat = WebFormat.FormUrlEncoded
    auth_Request.AddBodyParameter "grant_type", "authorization_code"
    auth_Request.AddBodyParameter "client_id", "<My ID>"
    auth_Request.AddBodyParameter "client_secret", "<My Secret>"
    auth_Request.AddBodyParameter "redirect_uri", "My Reidrect URI>"

    str_AuthorizationCode = Me.Text1
    auth_Request.AddBodyParameter "code", str_AuthorizationCode 

    Set auth_Response = auth_TokenClient.Execute(auth_Request)

    If auth_Response.StatusCode = WebStatusCode.Ok Then
        'GetToken = auth_Response.Data(Me.TokenKey)
        MsgBox "Hello"

    Else
        Err.Raise 11041 + vbObjectError, _
            Description:=auth_Response.StatusCode & ": " & auth_Response.Content
    End If

auth_Cleanup:

    Set auth_TokenClient = Nothing
    Set auth_Request = Nothing
    Set auth_Response = Nothing

    ' Rethrow error
    If Err.Number <> 0 Then
        Dim auth_ErrorDescription As String

        auth_ErrorDescription = "An error occurred while retrieving token." & vbNewLine
        If Err.Number - vbObjectError <> 11041 Then
            auth_ErrorDescription = auth_ErrorDescription & _
                Err.Number & VBA.IIf(Err.Number < 0, " (" & VBA.LCase$(VBA.Hex$(Err.Number)) & ")", "") & ": "
        End If
        auth_ErrorDescription = auth_ErrorDescription & Err.Description

        WebHelpers.LogError auth_ErrorDescription, "OAuth2Authenticator.GetToken", 11041 + vbObjectError
        Err.Raise 11041 + vbObjectError, "OAuth2Authenticator.GetToken", auth_ErrorDescription
    End If

I would greatly appreciate any help, I have been through the documentation and looked for answers elsewhere first before asking here. (Note: The authorization code I am pulling from a request from Postman.)

Thank you.

zgrose commented 4 years ago

I'm guessing we'd have to see the documentation/example on the way it is expecting the code to be sent?

DCNetSolutions commented 4 years ago

Certainly zgrose, I should have included it, my apologies. The whole thing is here: https://developer.sage.com/api/accounting/guides/authentication/

However the specific is: POST https://oauth.accounting.sage.com/token Content-Type: application/x-www-form-urlencoded

client_id=4b6xxxxxxx710 &client_secret=iNuxxxxxxxxxxtm9 &code=12axxxxxxxxxxxxf7d &grant_type=authorization_code &redirect_uri=https://myapp.com/auth/callback

zgrose commented 4 years ago

i don't recall off hand if the class will URLEncode for you, but so far looks right. If there are special characters in any of the parameters, try the post with a UrlEncode( ) around the parameter value?

DCNetSolutions commented 4 years ago

Zgroses Et al.,

I fixed the issue, I was looking at the wrong token returned by Postman and also had grabbed the wrong grant_type. I guess to many hours looking and tunnel vision equals egg on my face. Seems like the only time I find my own errors is when I have put it out there for the world to see. I cannot thank you enough for your prompt replies Zgrose and Mr. Hall for this and the other great VBA code you have put out for the world to see and use and those who have contributed to make it even better.