【问题标题】:How to do authentication onedrive for Business and upload a file (WinForms)如何对业务进行身份验证 onedrive 并上传文件 (WinForms)
【发布时间】: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


    【解决方案1】:

    据我所知,您不能在 Windows 客户端应用程序(包括 Windows 窗体应用程序)中对 SharePoint Online 使用 OAuth 身份验证。这个MSDN article 显示了在线 SharePoint 的 OAuth 流程:您需要一个服务器 URL 作为应用程序的注册重定向 URI,但 Windows 窗体应用程序没有。

    另一种方法是使用SharePoint client object model,下面的代码显示了如何访问OneDrive:

            string username = "xxx@xxx.onmicrosoft.com";
            String pwd = "xxx#";
            ClientContext context = new ClientContext("https://xxx-my.sharepoint.com/personal/xxx_xxxinc_onmicrosoft_com/");
    
            SecureString password = new SecureString();
            foreach (char c in pwd.ToCharArray())
            {
                password.AppendChar(c);            }
    
            context.Credentials = new SharePointOnlineCredentials(username, password);
            //login in to SharePoint online
            context.ExecuteQuery();   
    
            //OneDrive is acctually a Document list
            List docs = context.Web.Lists.GetByTitle("Documents");
            context.ExecuteQuery();
    
            CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
            ListItemCollection items = docs.GetItems(query);
    
            // Retrieve all items the document list
            context.Load(items);
            context.ExecuteQuery();
            foreach (ListItem listItem in items)
            {
                Console.WriteLine(listItem["Title"]);
            } 
    

    另外,你可以使用 REST APIs,这篇文章Access OneDrive for Business using the SharePoint 2013 APIs 解释了如何使用 REST 来做到这一点。

    【讨论】:

    • 非常感谢您的回答!我终于与我的 OneDrive 建立了联系。尽管我每次想要访问时都必须登录。登录后有没有办法获取刷新令牌?因此,下次用户使用我的应用程序时,我可以使用该刷新令牌来获取访问令牌,以便通过 CMOS 对 OneDrive 执行命令。
    • @Castrovalva,据我所知,Windows客户端应用程序没有这样的令牌,如果asp.net,你可以做到。
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多