微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – OneDrive客户端身份验证(SDK)

我在玩OneDrive SDK 1.1.15.0:

try
{
    AppConfig appConfig = new AppConfig
    {
        MicrosoftAccountAppId = oneDriveClientID,//something like 00000000123456AB
        MicrosoftAccountClientSecret = oneDriveClientSecret,//something like 3vx[...]1sJ
        MicrosoftAccountReturnUrl = "https://localhost/return",MicrosoftAccountScopes = new string[] { "wl.signin","wl.offline_access","onedrive.readonly" }
    };
    OneDriveClient oneDriveClient = new OneDriveClient(appConfig);
    AccountSession accountSession = await oneDriveClient.AuthenticateAsync();

    //more code

    await oneDriveClient.SignOutAsync();
}
catch (Exception ex)
{
    throw ex;
}

我的问题在于:

AccountSession accountSession = await oneDriveClient.AuthenticateAsync();

抛出以下异常:

Microsoft.OneDrive.Sdk.OneDriveException,AuthenticationFailure: Failed to retrieve a valid authentication token for the user.

有任何想法吗?

先感谢您!

UPDATE

在阅读了ginach的评论后(谢谢!),我更新了我的代码.一些要强调的论点:

>我想从Azure辅助角色访问OneDrive,因此没有身份验证窗口或类似的东西.
>我将Microsoft.OneDrive SDK上传到1.1.20版本.
>我已将我的应用程序注册到OneDrive开发门户.

我的实际代码是:

try
{
    MicrosoftAccountServiceInfo serviceInfo = new MicrosoftAccountServiceInfo();

    serviceInfo.AppId = oneDriveClientID; //something like: 00000000ABCDEFGH
    serviceInfo.ClientSecret = oneDriveClientSecret; //something like: 3vx[...]1sJ
    serviceInfo.ReturnUrl = oneDriveReturnUrl; //something like: https://localhost/return
    serviceInfo.Scopes = oneDriveAccountScopes; //something like new string[] { "wl.signin","onedrive.readonly" }

    MicrosoftAccountAuthenticationProvider authenticationProvider = new MicrosoftAccountAuthenticationProvider(serviceInfo);

    OneDriveClient oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(oneDriveClientID,oneDriveReturnUrl,oneDriveAccountScopes,authenticationProvider);

    //more code

    await oneDriveClient.SignOutAsync();
}
catch (OneDriveException odex)
{
    throw odex;
}
catch (Exception ex)
{
    throw ex;
}

我一次又一次地获取(在OneDriveClient.GetAuthenticatedMicrosoftAccountClient方法中)OneDriveException说明(错误属性):AuthenticationFailure – 无法为用户检索有效的身份验证令牌.

有什么建议吗?

谢谢.

更新2

好的,我正在尝试一种新的方法.使用RestSharp我尝试使用该代码登录OneDrive:

string clientId = "00[...]00";
string scopes = "wl.signin,wl.offline_access,onedrive.readonly";
string responseType = "code";
string redirectUri = "https://login.live.com/oauth20_desktop.srf";

RestClient client = new RestClient("https://login.live.com");
RestRequest request = new RestRequest();

request.Method = Method.GET;
request.Resource = "oauth20_authorize.srf";
request.AddQueryParameter("client_id",clientId);
request.AddQueryParameter("scope",scopes);
request.AddQueryParameter("response_type",responseType);
request.AddQueryParameter("redirect_uri",redirectUri);

IRestResponse response = client.Execute(request);
string content = response.Content;

我用fiddler检查请求,我发送的是:

https://login.live.com/oauth20_authorize.srf?client_id=00[...]00&scope=wl.signin%20wl.offline_access%20onedrive.readonly&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf

但OneDrive服务器回答我:

Microsoft帐户需要JavaScript才能登录.此Web浏览器不支持JavaScript,或者脚本被阻止.要了解您的浏览器是否支持JavaScript,还是允许脚本,请参阅浏览器的在线帮助.

所以我在浏览器中尝试请求,OneDrive服务器将我重定向到授权页面

enter image description here

现在的问题是:是否有任何解决方法可以跳过手动授权?

谢谢,
阿蒂利奥

解决方法

客户端要求身份验证提供程序能够检索身份验证令牌.根据您当前的平台,有几种方法可以做到这一点.

>创建自己的IAuthenticationProvider实现.身份验证提供程序负责在请求上设置Authentication头.以下是使用自定义身份验证提供程序创建客户端实例的方法

var client = new OneDriveClient(appConfig,serviceInfoProvider: new
ServiceInfoProvider(new CustomAuthenticationProvider()));

>使用各种认身份验证实现之一.请查看SDK authentication documentation获取可用选项和示例.
>如果您有刷新令牌并且只想执行静身份验证流,则可以使用OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient.这是一个例子:

var client = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(clientId,returnUrl,scopes,refreshToken);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐