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

This thread is now closed to new comments.
Some of the links and information provided in this thread may no longer be available or relevant.
If you have a question please start a new post.
Kruts
Experienced Partner
13 Posts
Experienced Partner
Experienced Partner

13Posts

0Kudos

0Solutions

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 18
kjm
Trusted Cover User
160 Posts
Trusted Cover User
Australia
Trusted Cover User

160Posts

18Kudos

2Solutions

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

posting from my other now linked dev account

 

I have solved this myself. little bit "surprised" at the lack of support both from myob and community. maybe these forums arent that busy/alive..

 

anyway..got the access code and passed it in..looks like ths SDK has a problem from there on in as I am returned a Bad Request

I have built own classes to extract the tokens using a httpListener and it works fine whic his why Ican verify that I am getting the correct access code

 

If anyone gets stuck like I did PM and I'll be happy to share code..

PhilWherrett
Experienced User
11 Posts
Experienced User
Experienced User

11Posts

5Kudos

0Solutions

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

Howdy Again @kjm ,

 

I've just started on this implementation myself - sounds like we've headed down the same path using HttpListener - but I'm not sure how to handlethe redirect if the redirect Url is http://desktop as per MYOBs recommendation when creating the API Key.  I can get a code using http://localhost as a redirect Url.

 

I'd be keen to get code examples of your implementation if you're willing to share (I can't seem to work out how to send you a PM :-()?

 

Cheers,

Phil.

kjm
Trusted Cover User
160 Posts
Trusted Cover User
Australia
Trusted Cover User

160Posts

18Kudos

2Solutions

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

Hi @PhilWherrett 


To PM I think just hover over avatar and click on "Send Message" 

 

I have the redirect set to http://127.0.0.1  I suppose you could modify the host file so that http://desktop resolves to this or localhost but not sure why myob are recommending that. Dont you simply need a socket for the httpListener so that it know where to listen for the response?



I have this in a in a base class 

public class BaseAuthenticator
    {

        // ref http://stackoverflow.com/a/3978040
        private static int GetRandomUnusedPort()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            var port = ((IPEndPoint)listener.LocalEndpoint).Port;
            listener.Stop();
            return port;
        }

        public string GetRedirectUri => $"http://{IPAddress.Loopback}:{GetRandomUnusedPort()}/";
        
    }

 

 private string GetAuthorizationCode()
        {
            var redirectUri = GetRedirectUri;
            Output("redirect URI: " + redirectUri);

            // Creates an HttpListener to listen for requests on that redirect URI.
            var httpListener = new HttpListener();
            httpListener.Prefixes.Add(redirectUri);
            Output("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),
                ClientId);

            // Opens request in the browser.
            System.Diagnostics.Process.Start(authorizationRequest);

            // Waits for the OAuth authorization response.
            var context = httpListener.GetContext();
var response = context.Response; }

then get the response and parse it for the code

happy to give you the whole code if you're still stuck

PhilWherrett
Experienced User
11 Posts
Experienced User
Experienced User

11Posts

5Kudos

0Solutions

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

Howdy @kjm ,

 

Yep I tried to do that for the PM - but the page I get sent to doesn't render properly to allow sending anything:

 

Screenshot 2022-09-27 162640.png

 

Your code snippets are pretty much what I'm doing so thanks for sharing and confirming that I'm heading in the right direction :-)

 

I've managed to successfully get a code response and return the company files available so that's good.

 

Now just need to add the managing of token storage and refresh lifecycle so subsequent calls don't need to go through additional code requests.

 

Cheers,

Phil.

John_Dinning
Trusted User
71 Posts
Trusted User
Australia
Trusted User

71Posts

16Kudos

6Solutions

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

Hi Kruts,

 

Sorry for the late reply, I have not been monitoring this forum. I have OAuthLogin.cs and OAuthKeyService.cs that I believe were originally included in the C# samples provided by MYOB on their github which doesn't seem to exist any more. 

As far as I know, these are unmodified and still work. Hopefully MYOB won't mind me attaching these. Perhaps they could make them accesible to others again.

 

 

Hamishjam
4 Posts
User

4Posts

1Kudos

0Solutions

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

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; }
John_Dinning
Trusted User
71 Posts
Trusted User
Australia
Trusted User

71Posts

16Kudos

6Solutions

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

Hi Hamishjam,

 

The lost OAuthLogin.GetAuthorizationCode is in the zip file attached to my last post.

Let me know if you can't get access to it.

Hamishjam
4 Posts
User

4Posts

1Kudos

0Solutions

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

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

Hamishjam
4 Posts
User

4Posts

1Kudos

0Solutions

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

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.

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