看起来这应该很简单,但是我找不到一个示例或足够详尽的文档来弄清楚它。
我有一个C#桌面应用程序,我想通过Box API与Box集成。我认为使用Box Windows SDK v2 for .NET将是必经之路。
有人可以指出一个简单的,适用于桌面应用程序的简单例子吗?
我决定尝试自己解决这个问题。即使OAuth2支持基于非浏览器的身份验证,Box.com显然也决定不实施它(或者,至少在任何地方我都没有提及如何实现它)。
因此,基于桌面的应用程序的唯一替代方法是以某种方式截获发生的URL重定向,并从查询字符串参数中提取身份验证信息。
但是,由于IE最近落后于时代,并且我正在使用C#和.NET,因此我决定考虑嵌入其他浏览器,而不是使用内置的浏览器控件。我选择了Awesomium,这是一个托管的.NET Chromium包装器。
因此,事不宜迟,我提出了适用于桌面应用程序的基本示例。
我的解决方案有两种形式,一种是纯粹用作“浏览器”的形式,另一种是主要形式:frmMain包含所有代码,而frmBrowser包含Awesomium控件。
using Newtonsoft.Json.Linq;
using System.Web;
private static frmBrowser browser = null;
private const string BoxClientId = "{your client id}";
private const string BoxSecret = "{your secret}";
private void authenticateWithBox()
{
browser = new frmBrowser();
browser.Show();
browser.webControl1.Source = new Uri("https://www.box.com/api/oauth2/authorize?response_type=code&client_id=" + BoxClientId + "&redirect_uri=https://localsess");
browser.webControl1.AddressChanged += new Awesomium.Core.UrlEventHandler(webControl1_AddressChanged);
}
void webControl1_AddressChanged(object sender, Awesomium.Core.UrlEventArgs e)
{
//MessageBox.Show(e.Url.ToString());
if (e.Url.Host == "localsess")
{
NameValueCollection parms = HttpUtility.ParseQueryString(e.Url.Query);
if (parms.AllKeys.Contains("error"))
{
MessageBox.Show("Error connecting to Box.com: " + parms["error"] + " " + parms["error_description"]);
}
else
{
boxContinue(parms["code"]);
}
}
}
上面的代码就是神奇的地方。每次Web控件显示的URL更改时,都会触发AddressChanged事件。因此,您必须将重定向URL设置为可以检测到的唯一内容-甚至不必存在,如示例代码所示。然后,您可以提取所需的参数并继续进行身份验证过程。
string postToUrl(string url, string data)
{
string results = String.Empty;
WebRequest req = WebRequest.Create(url);
req.Method = WebRequestMethods.Http.Post;
byte[] byteArray = Encoding.UTF8.GetBytes(data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = byteArray.Length;
Stream dataStream = req.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse res = req.GetResponse();
dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
results = reader.ReadToEnd();
return results;
}
void boxContinue(string code)
{
browser.Close();
browser.Dispose();
string json = postToUrl("https://www.box.com/api/oauth2/token", "code=" + code + "&grant_type=authorization_code&client_id=" + BoxClientId + "&client_secret=" + BoxSecret);
JToken token = JObject.Parse(json);
string access_token = (string)token.SelectToken("access_token");
string refresh_token = (string)token.SelectToken("refresh_token");
}
void boxRefresh(string refresh_token)
{
string json = postToUrl("https://www.box.com/api/oauth2/token", "grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + BoxClientId + "&client_secret=" + BoxSecret);
JToken token = JObject.Parse(json);
string access_token = (string)token.SelectToken("access_token");
string new_refresh_token = (string)token.SelectToken("refresh_token");
}
其余代码只是您的常规身份验证代码,该代码使用令牌和先前请求中的令牌获取更多令牌等。Box使用“ refresh_tokens”使您能够获取其他访问令牌,我展示了一个如何做到这一点的例子。
如果您发现任何错误或有任何评论等,请发表评论。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句