Last active
March 2, 2019 04:39
-
-
Save sawilde/247533057916bcafb6d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
using System.Windows.Forms; | |
using MYOB.AccountRight.SDK; | |
using MYOB.AccountRight.SDK.Communication; | |
using MYOB.AccountRight.SDK.Services; | |
namespace arl_winforms_sample | |
{ | |
public partial class Form1 : Form | |
{ | |
private readonly Uri _authorizeUri = new Uri( | |
string.Format("https://secure.myob.com/oauth2/account/authorize?client_id={0}&redirect_uri={1}&scope=CompanyFile&response_type=code", | |
Configuration.ClientId, Uri.EscapeDataString(Configuration.RedirectUrl))); | |
private readonly Uri _logoffUri = new Uri("https://secure.myob.com/oauth2/account/logoff"); | |
private readonly SimpleOAuthKeyService _keyService = new SimpleOAuthKeyService(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
protected override void OnLoad(EventArgs e) | |
{ | |
base.OnLoad(e); | |
webBrowser1.Navigate(_authorizeUri); | |
} | |
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) | |
{ | |
var content = webBrowser1.DocumentText; | |
var regex = new Regex(@"\<title\>(.+?)=(.+?)\</title\>"); | |
var match = regex.Match(content); | |
if (!match.Success || match.Groups.Count != 3) | |
return; | |
switch (match.Groups[1].Value.ToLowerInvariant()) | |
{ | |
case "code": // we have a code | |
var code = match.Groups[2].Value; | |
var config = new ApiConfiguration(Configuration.ClientId, Configuration.ClientSecret, Configuration.RedirectUrl); | |
var service = new OAuthService(config, new WebRequestFactory(config)); | |
var tokens = service.GetTokensAsync(code).Result; // <= blocking | |
_keyService.OAuthResponse = tokens; | |
break; | |
case "error": // user probably said "no thanks" | |
webBrowser1.Navigate(_logoffUri); | |
break; | |
} | |
} | |
} | |
public class Configuration | |
{ | |
public const string ClientId = "####"; | |
public const string ClientSecret = "****"; | |
public const string RedirectUrl = "http://desktop"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment