【发布时间】:2014-08-15 23:22:53
【问题描述】:
我正在尝试制作一个应用程序,该应用程序使用我写入 Microsoft Office 365 OneDrive for Business 的 C# 代码上传文件。
我尝试了几种方法来获取刷新令牌和访问令牌。
但我找不到如何在网上实现这一点的方法。
我尝试使用 this 博客解释如何使用 REST 进行身份验证。
这是我到目前为止所得到的:
private void btnAuthenticate_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(GetAuthorizationUrl());
}
private string GetAuthorizationUrl()
{
// Create a request for an authorization code.
string url = string.Format(" {0}common/oauth2/authorize?&response_type=code&client_id={1}&resource={2}&redirect_uri={3}&state={4}",
_authorizationEndpoint,
_clientId,
_resource,
_redirectURI,
_state);
return url;
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.AbsoluteUri.Contains("code="))
{
var splited = e.Url.AbsoluteUri.Split(new char[] { '=', '&' });
_authorizationInformation.Code = splited[1];
_authorizationInformation.SessionState = splited[3];
if (_authorizationInformation.SessionState.Equals(_state))
{
GetTokenInformation(_authorizationInformation);
}
}
}
private TokenInformation GetTokenInformation(AuthorizationInformation authInformation)
{
try
{
var response = Post(HttpUtility.UrlEncode(_tokenEndpoint), new NameValueCollection(){
{ "grant_type", "authorization_code" },
{ "code", authInformation.Code },
{ "redirect_uri", _redirectURI },
{ "client_id", _clientId },
{ "client_secret", _clientSecret },
});
Stream responseStream = new MemoryStream(response);
using (var reader = new StreamReader(responseStream))
{
var json = reader.ReadToEnd();
_tokenInformation = JsonConvert.DeserializeObject<TokenInformation>(json);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error" + exception.HResult.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
在 GetTokenInformation 方法中,响应始终为空 0 字节。
**第一个问题:如何正确使用 OAuth 对 onedrive pro 进行身份验证?**
**第二个问题:获得accesstoken后如何上传文件?**
【问题讨论】:
标签: c# winforms authentication oauth office365