从桌面应用程序访问OneDrive

时间:2016-06-28 15:32:27

标签: c# onedrive

我正在尝试使用本教程来掌握OneDrive:

https://msdn.microsoft.com/en-us/library/hh826529.aspx

当我在代码中运行时,它会获得makeAccessTokenRequest函数,发送以下requestURL:

“https://login.live.com/oauth20_token.srf?client_id=[myclientID]& client_secret = [myclientsecret]& redirect_uri = https:// login.live.com/oauth20_desktop.srf&grant_type= authorization_code&安培;代码= [AUTHCODE]“

(请忽略“https:”之后的空格,我必须在这里添加它们才能提出问题)

[myclientid],[myclientsecret]和[authcode]似乎都正确填充。它似乎得到响应,因为它运行“accessToken_DownloadStringCompleted”函数,但抛出“TargetInvocationException”错误,错误的内部消息是“”远程服务器返回错误:(400)错误请求。“。

有人可以对此发光吗?我对此完全陌生,如果我的问题毫无意义,或者令人生气的含糊,那么道歉......

1 个答案:

答案 0 :(得分:0)

对oauth20_token.srf端点的请求需要是一个POST,其中包含帖子正文中的参数,而不是查询字符串。由于您没有提到用于构建HTTP请求的代码,因此很难提供示例,但请查看我的示例OAuth 2项目中的RedeemAuthorizationCodeAsync以获取想法。

传出请求应如下所示:

POST https://login.live.com/oauth20_token.srf
Content-Type: application/x-www-form-urlencoded

client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}&code={code}&grant_type=authorization_code

您可能还会发现本教程比您链接的教程更容易:https://dev.onedrive.com/auth/msa_oauth.htm

如果您正在使用OneDrive(您标记了OneDrive的帖子),那么您可能需要考虑使用OneDrive SDK。它包括对几种类型的.NET项目的身份验证,因此您无需弄清楚如何自己进行身份验证。