Forum Discussion

Kruts's avatar
Kruts
Experienced User
3 years ago

doco makes reference to OAuthLogin.GetAuthorizationCode...where is this class?

following example here Cloud Service – Support for the MYOB family of SME product APIs

and have installed the SDK but it is not finding OAuthLogin class from line 6 below

 

what do I need to reference?

 

var developerKey = "YOUR API KEY";
var developerSecret = "YOUR API SECRET";

var configuration = new ApiConfiguration(developerKey, developerSecret, "http://desktop");
var oauthService = new OAuthService(configuration);
var tokens = oauthService.GetTokens(OAuthLogin.GetAuthorizationCode(configuration));
var keystore = new SimpleOAuthKeyService();
keystore.OAuthResponse = tokens;

// Get Company Files
var cfService = new CompanyFileService(configuration, null, keystore);
var companyFiles = cfService.GetRange();

 

 

it says : The function OAuthLogin.GetAuthorizationCode is a helper class created in the samples that illustrates how to create a form that launches the secure.myob.com my.MYOB login page.

 

what samples?? 

18 Replies

Replies have been turned off for this discussion
  • Hamishjam's avatar
    Hamishjam
    Contributing User
    3 years ago

    Hey, I also wound up in MYOB website 404 land sorting this out.  I've played with the code from KJM to spit out the auth code in a .NET core WPF app.  It's a bit rough at this stage but seems to do the trick as a replacement for the lost OAuthLogin.GetAuthorizationCode:

     

    private string GetAuthorizationCode()
    {
    
    var redirectUri = $"http://localhost:8080/";
    var developerKey = "blah blah";
    
    var AuthorizationCodeEndpoint = "https://secure.myob.com/oauth2/account/authorize";
    
    // Creates an HttpListener to listen for requests on that redirect URI. var httpListener = new HttpListener(); httpListener.Prefixes.Add(redirectUri); // Listening httpListener.Start(); // Creates the OAuth 2.0 authorization request. var authorizationRequest = string.Format("{0}?response_type=code&scope=CompanyFile&redirect_uri={1}&client_id={2}", AuthorizationCodeEndpoint, System.Uri.EscapeDataString(redirectUri), developerKey); // Opens request in the browser. var ps = new ProcessStartInfo(authorizationRequest) { UseShellExecute = true, Verb = "open" }; Process.Start(ps); //System.Diagnostics.Process.Start(authorizationRequest); // Waits for the OAuth authorization response. var context = httpListener.GetContext(); var request = context.Request; httpListener.Stop(); var uri = new Uri(request.Url.OriginalString); var query = System.Web.HttpUtility.ParseQueryString(uri.Query); var code = query.Get("code"); return code; }
  • Hamishjam's avatar
    Hamishjam
    Contributing User
    3 years ago

    Cheers John - I came back to the forum and saw you posted this after I spent an hour stuffing around with it :) 

  • Hamishjam's avatar
    Hamishjam
    Contributing User
    3 years ago

    I'm now having issues with the CompanyFileService.GetRange() method.  Using:

     

    // Get Company Files
                var companyFileService = new MYOB.AccountRight.SDK.Services.CompanyFileService(configuration,null,oAuthKeyService);
                var companyFiles = companyFileService.GetRange();

    GetRange() is throwing the following exception:

    MYOB.AccountRight.SDK.ApiOperationException: 'Encountered an operation error (https://secure.myob.com/oauth2/account/authorize/)'

     

    Inner Exception:

    JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 2, position 1

     

    I'm working in a .NET 6 WPF solution.

  • kjm's avatar
    kjm
    Trusted Cover User
    3 years ago

    Have you got the access token? Hamishjam 

    I have not used the class that was uploaded by John_Dinning  so I cannot comment on whether it works or not but I have built my own class to get the access token

    maybe that was why the class was removed from the online sample but like I said I have not tried using it..

    If you have the access token then just create a SimpleOAuthKeyService and assign the values from the token to it and you should be able to run the following.  

    let me know how you get on. If I have time I'll build a test harness and upload it to github for others that are stuck here...seriously considering leaving the .NET SDK and just using RestSharp/HttpClient to access the API

     

      var keystore = new SimpleOAuthKeyService();
                    keystore.OAuthResponse = new OAuthTokens
                    {
                        AccessToken = myobTokens.AccessToken,
                        ExpiresIn = int.Parse(myobTokens.ExpiresIn),
                        ReceivedTime = myobTokens.TokenTime,
                        RefreshToken = myobTokens.RefreshToken,
                        Scope = myobTokens.Scope,
                        TokenType = myobTokens.TokenType
                    };
    
    
    
                    // Get Company Files
                    var cfService = new CompanyFileService(configuration, null, keystore);
                    var companies = cfService.GetRange();
    
                    foreach (var company in companies)
                    {
                        System.Console.WriteLine(company.Name);
                        Debug.WriteLine(company.Name);
                    }

     

  • kjm's avatar
    kjm
    Trusted Cover User
    3 years ago

     

     

    I am coding in Winforms/.NET 4.8 and it works fine but I am also leaning towards removing the .NET SDK

     

     

     

  • The_Doc's avatar
    The_Doc
    Ultimate Cover User
    3 years ago

    Hi kjm 

     

    Even though I am a C#.NET, VB.NET programmer I decided long ago NOT to use the SDK - just long experience with MYOB.

     

    I designed my own - using MS Access VBA - and they work almost without problem - but the problem isn't usually the API. And they just work.

     

    However, now I need this code in VC#.NET - hmm guess I got to go for writing my own code in C# - which shouldn't be a problem as the nitty gritty of getting it all working in VBA is long ago worked out.

     

    I get very few API errors - but mainly if I do they are timeouts and I just recycle.

     

    The Doc

  • kjm's avatar
    kjm
    Trusted Cover User
    3 years ago

    the more interesting point is that the SDK uses HttpWebRequest in  WebRequestFactory.cs class to make its calls.

     

    Pretty sure MS are  phasing out WebRequest and will only use HttpClient...or hasnt it already been deprecated?

     

    make a point for moving away from the SDK to using RestSharp or just straight HttpClientFactory to the API...seems cleaner

     

     Hamishjam