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服务器将我重定向到授权页面:
现在的问题是:是否有任何解决方法可以跳过手动授权?
谢谢,
阿蒂利奥
>创建自己的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);
以上是内存溢出为你收集整理的c# – OneDrive客户端身份验证(SDK)全部内容,希望文章能够帮你解决c# – OneDrive客户端身份验证(SDK)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)