我正在玩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.
有什么想法吗?
提前谢谢!
更新
在阅读ginach的评论后(谢谢!),我更新了我的代码。一些要强调的论点:
我的实际代码是:
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", "wl.offline_access", "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服务器将我重定向到授权页面:
现在的问题是:是否有任何解决方法可以跳过手动授权?
谢谢你, 阿蒂利奥
答案 0 :(得分:1)
客户端要求身份验证提供程序能够检索身份验证令牌。根据您当前的平台,有几种方法可以做到这一点。
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);