【问题标题】:Upload to OneDrive with Offline Access使用离线访问上传到 OneDrive
【发布时间】:2014-11-26 06:28:12
【问题描述】:

我创建了一个 PCL 项目以使用 REST API(上传/下载/文件夹管理等)与 OneDrive 交互。我遇到的问题是在用户离线时能够使用 OneDrive,其他一切正常。

当我重定向用户启动 OAUTH2 身份验证时,我使用以下 Url

string.Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&state={1}&response_type=code&redirect_uri={2}&scope=wl.basic wl.signin wl.offline_access wl.skydrive_update", _microsoftClientId, userId, Url.AbsoluteAction("CallBack", "Microsoft"))

用户登录并授权应用后出现以下错误。

There's a temporary problem

There's a temporary problem with the service. Please try again. If you continue to get this message, try again later.

如果我将范围更改为仅包含 wl.skydrive_update,它可以工作,但我没有获得 refresh_token。我尝试了不同的范围,使用不同的分隔符(%20、逗号、空格)登录和授权后总是出现上述错误。

我被卡住了。

【问题讨论】:

    标签: rest oauth-2.0 onedrive


    【解决方案1】:

    看起来微软最终解决了这个问题。没有任何代码更改,它现在可以工作。

    我现在有一个令牌和一个刷新令牌。

    这就是我重定向到 Microsoft Live 的方式

    Response.RedirectPermanent(string.Format("https://login.live.com/oauth20_authorize.srf?client_id={0}&state={1}&response_type=code&redirect_uri={2}&scope=wl.signin%20wl.skydrive_update%20wl.offline_access", _microsoftClientId, userId, Url.AbsoluteAction("CallBack", "Microsoft")));
    

    我的回调操作

            public RedirectToRouteResult CallBack(string code, string state, string error, string error_description)
            {
                logger.Debug("Callback from Micrsoft");
                if (string.IsNullOrWhiteSpace(error))
                {
                    var client = new RestClient("https://login.live.com");
                    var request = new RestRequest("oauth20_token.srf", Method.POST);
                    request.AddParameter("grant_type", "authorization_code");
                    request.AddParameter("code", code);
                    request.AddParameter("client_id", _microsoftClientId);
                    request.AddParameter("client_secret", _microsoftClientSecret);
                    request.AddParameter("redirect_uri", Url.AbsoluteAction("CallBack", "Microsoft"));
    
                    logger.Debug("POSTING to Micrsoft");
                    var mslResponse = client.Execute<MicrosoftLiveResponse>(request);
    
                    if (mslResponse != null && mslResponse.Data != null)
                    {
                        logger.Debug("RESPONSE: " + mslResponse.Content);
    
                        var mslClient = mslResponse.Data;
                        if (string.IsNullOrWhiteSpace(mslClient.error))
                        {
    
    //Update the database and redirect to the Done Action
    if (OauthBL.UpdateMicrosoftLiveToken(Utilities.ConvertToObjectId(state, ObjectId.Empty), mslClient.access_token, mslClient.refresh_token, mslClient.expires_in))
                            {
                                return RedirectToAction("Done");
                            }
                        }
                    }
                }
                logger.Debug("INITIAL ERROR:" + error + " - " + error_description);
                return RedirectToAction("Error");
            }
    

    还有我的 MicrosoftLiveResponse 实体

    namespace Entities
    {
        public class MicrosoftLiveResponse
        {
            public string access_token { get; set; }
    
            public string authentication_token { get; set; }
    
            public string token_type { get; set; }
    
            public int expires_in { get; set; }
    
            public string refresh_token { get; set; }
    
            public string uid { get; set; }
    
            public string error { get; set; }
    
            public string error_description { get; set; }
        }
    }
    

    希望这对某人有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多