MYOBCommunity
Partner Zone
|
|
Partner Zone

Cloud Authentication Issues (403 Forbidden)

This thread is now closed to new comments.
Some of the links provided in this thread may no longer be available or relevant.
If you have a question please start a new post.
2 REPLIES 2
Partner darcher
4 Posts
Partner
Australia
darcher
Partner

4Posts

0Kudos

1Solutions

Accepted Solution Solved Solution

Re: Cloud Authentication Issues (403 Forbidden)

Okay,

 

By trawliong through community posts, I've managed to sort out (I think) my issues.

 

Issues with Developer Keys

According to another post, if you have more than 2 developer applications, the 3rd set will not authenticate without being approved by the MYOB developer team.  I've spent a week trying to work out why my keys were getting an error, thinking i was doing something wrong.  There is no information about this on your portal page, or even an option to get a developer key approved.  

 

I'm now using one of my original app keys and all works fine.

 

As a suggestion, I think the Developer Team could make this issue a little more visible in the API Keys page.  It would have saved me days!

 

Refreshing Tokens

This turned out to be pretty easy with the SDK.  Below is a sample of the code I used that seemed to work.  I couldn't find any examples but the below works.

 

_configurationCloud = New ApiConfiguration(objDevKey.DeveloperKey, objDevKey.DeveloperSecret, "http://desktop")
myOAuthKeyService = New OAuthKeyService()                 
Dim authTokens As New OAuthTokens
'Use code to retrieve token from database store and set to authTokens

myOAuthKeyService.OAuthResponse = authTokens

'Get tokens if not stored or have expried
If myOAuthKeyService.OAuthResponse.HasExpired = True Or ForceTokenRefresh = True Then
    
    Dim oauthService As New OAuthService(_configurationCloud)
    Dim tokens As New OAuthTokens
    Dim bolError As Boolean = false

    'Try and refresh tokens first ...
    Try
        'Renew tokens - uses existing token to get a new token
        tokens = oauthService.RenewTokens(myOAuthKeyService.OAuthResponse)
        bolError = False    
    Catch exRefresh As Exception
        'Refreshing the tokens failed - will need to login manually to get new access token
        bolError = True        
    End Try

    'manually retrieve tokens
    If bolError = True Then
        Try
            tokens = oauthService.GetTokens(OAuthLogin.GetAuthorizationCode(_configurationCloud))
            bolError = False
        Catch exNewToken As Exception
            bolError = True
            _raygunClient.Send(exNewToken)

        End Try

    End If
    
    myOAuthKeyService.OAuthResponse = tokens
Partner darcher
4 Posts
Partner
Australia
darcher
Partner

4Posts

0Kudos

1Solutions

Re: Cloud Authentication Issues (403 Forbidden)

Code used to get company files:

 

When getting local company files, this works fine.  When trying to access Cloud based files. I get the 'The remote server returned an error: (403) Forbidden.' error.

 

 

Imports MYOB.AccountRight.SDK
Imports MYOB.AccountRight.SDK.Services
Imports MYOB.AccountRight.SDK.Contracts
Imports Newtonsoft.Json
Imports Ripteq.myobSyncData.BusinessObjects
Imports Mindscape.Raygun4Net

Public Class userCompanyFiles
    Delegate Sub SetDataSourceCallback(companyFiles() As CompanyFile)

    Private Const LocalApiUrl As String = "http://localhost:8080/accountright"
    Private Const DeveloperKey As String = "xxxxx"
    Private Const DeveloperSecret As String = "xxxxxx"
    'Private Const useCloud As Boolean = False

    Public Const NetworkApiPart3 As String = ":8080/accountright" 'http://[ip address]:8080/accountright
    Public Const NetworkApiPart1 As String = "http://"
    Public Const CLOUD_API_FILEPATH As String = "https://api.myob.com/accountright/"
    
    Private _configurationCloud As IApiConfiguration
    Private _configurationLocal As IApiConfiguration
    Private _oAuthKeyService As IOAuthKeyService

    Private _connectionType As Integer = 0 '0=Local, 1=Network, 2=Cloud
    Public _raygunClient As RaygunClient = New RaygunClient(My.Settings.RGKey)


    Public Property ConnectionType As Integer
        Get
            Return _connectionType
        End Get
        Set(ByVal value As Integer)
            _connectionType = value
        End Set
    End Property

    Private Sub userCompanyFiles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCompanyFiles()
    End Sub

    Private Sub LoadCompanyFiles()
        Try
            Select Case ConnectionType
                Case 0
                    'Local
                    _configurationLocal = New ApiConfiguration(LocalApiUrl)
                    Dim cfsLocal = New CompanyFileService(_configurationLocal)
                    cfsLocal.GetRange(AddressOf OnComplete, AddressOf OnError)

                Case 1
                    'Network
                    _configurationLocal = New ApiConfiguration(Me.LabelNetwork.Text)
                    Dim cfsLocal = New CompanyFileService(_configurationLocal)
                    cfsLocal.GetRange(AddressOf OnComplete, AddressOf OnError)

                Case 2
                    'Cloud
                    _configurationCloud = New ApiConfiguration(DeveloperKey, DeveloperSecret, "http://desktop")
                    _oAuthKeyService = New OAuthKeyService()

                    'Get tokens if not stored
                    If _oAuthKeyService.OAuthResponse Is Nothing Then
                        Dim oauthService As New OAuthService(_configurationCloud)
                        _oAuthKeyService.OAuthResponse = oauthService.GetTokens(OAuthLogin.GetAuthorizationCode(_configurationCloud))
                    End If

                    Dim cfsCloud = New CompanyFileService(_configurationCloud, Nothing, _oAuthKeyService)
                    
                    'cfsCloud.GetRange(AddressOf OnComplete, AddressOf OnError)
                    Dim collFiles = cfsCloud.GetRange()
                    SetDataSource(collFiles)
                    
                Case Else
                    
            End Select

        Catch ex As ApiCommunicationException
            Dim strErrs As New List(Of String)

            For Each err As [Error] In ex.Errors
                Dim strE As String = String.Format("== Code:{0} Name:{1}{5}Severity: {2}{5}Message: {3}{5}{4}", err.ErrorCode, err.Name, err.Severity, err.Message, err.AdditionalDetails, vbcrlf)
                strErrs.Add(strE)
            Next

            _raygunClient.Send(ex, strErrs)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Highlighted
Partner darcher
4 Posts
Partner
Australia
darcher
Partner

4Posts

0Kudos

1Solutions

Solved: Go to Solution

Cloud Authentication Issues (403 Forbidden)

Hi,

 

I've developed my first API application and its working fine in local machine mode.  I'm trying to add the ability to use a Cloud account.

 

I've used the code from the VB.NET sample application but I'm running into a couple of issues.

 

1. Issue Getting List of Company Files

The first is when I try and get a list of company files.

 

I've registered an App with the redirect http://desktop

 

When I use the sample code with this key, I get an error:

Encountered a communication error (https://api.myob.com/accountright)

The remote server returned an error: (403) Forbidden.

 

A developer friend of mine sent me a code sample with his keys - when I use his keys and my code, the code works and I can retrieve a list of company files.

 

Is there something wrong with my developer account?  There was an issuing when this was setup.

 

2. List of Contacts

If I use my friends sample keys to access the Cloud account and retrieve a list of contacts, this works for a while and then fails.  Does this mean I need a mechanism to update the access tokens?  

 

Cheers,

David. 

 

 

Didn't find your answer here?
Try using advanced search to find a post more easily Advanced Search
or
Get the conversation started and make a new post Start a Post