Fai*_*aza 3 authentication api rest offline onedrive
我正在使用 OneDrive api 在我的 Ruby on Rails 应用程序中上传文件,而 OneDrive API 开始在使用端点 /drive/root:/#{filename}:/content 上传文件时出现未经身份验证的错误。错误如下:
{"error"=>{"code"=>"unauthenticated", "message"=>"Must be authenticated to use '/drive' syntax"}}
Run Code Online (Sandbox Code Playgroud)
然后我通过使用范围 files.readwrite offline_access 遵循 OneDrive Docs 获得了一个新的 refresh_token。
对于 OneDrive 身份验证,我将 POST 请求发送到端点https://login.microsoftonline.com/common/oauth2/v2.0/token以使用带有以下标头和正文的 refresh_token 获取 access_token:
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
body = {
'client_id' => "<Client ID>",
'grant_type' => "refresh_token",
'redirect_uri' => "<Redirect URI>",
'client_secret' => "<Client Secret>",
'refresh_token' => "<Refresh Token>",
}
Run Code Online (Sandbox Code Playgroud)
我是否使用正确的端点从 refresh_token 获取 access_token?
我用来将文件上传到 OneDrive 的基本 uri 是https://api.onedrive.com/v1.0
任何人都可以帮助我为什么我收到未经身份验证的错误或者我如何使用“/驱动器”语法进行身份验证?
提前致谢!
解决了:
就我而言,我使用“代码流”进行身份验证并使用以下 url 获取code参数:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=CLIENT_ID&scope=files.readwrite offline_access&response_type=code&redirect_uri=REDIRECT_URI
Run Code Online (Sandbox Code Playgroud)
访问上述网址,打开了一个长的重定向URL code,我用得到的参数access_token,并refresh_token在疑问,是的access_token不工作的文件上传到OneDrive,并重新调整提及的“未”的错误。
经过研究,我发现我用来获取codeOneDrive 身份验证的 url是针对 Microsoft Graph。Microsoft 帐户的正确 url 如下所示:
https://login.live.com/oauth20_authorize.srf?client_id=CLIENT_ID&scope=onedrive.readwrite offline_access&response_type=code&redirect_uri=REDIRECT_URI
Run Code Online (Sandbox Code Playgroud)
在浏览器中访问上述 url 也将我重定向到带有代码参数的页面,但它是像K9vb4e786-afg6-1a3b-1234-12abc01234ca.
我使用此代码获取access_token并refresh_token使用以下 POST 请求:
body = {
client_id: "CLIENT_ID",
redirect_uri: "REDIRECT_URI",
client_secret: "CLIENT_SECRET",
code: "CODE",
grant_type: "authorization_code"
}
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
r=HTTParty.post('https://login.live.com/oauth20_token.srf', headers: headers, body: body)
Run Code Online (Sandbox Code Playgroud)
此请求返回 access_token 和 refresh_token 作为响应。我用这个refresh_token来得到一个access_token在每个请求和文件上传成功。
结论:我使用的是 Microsoft Graph 身份验证过程,即https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth,这是不正确的。然后我遵循 Microsoft 帐户身份验证,即https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/msa-oauth解决了该问题。
更新:
后来我使用我的 Office-365 商业帐户进行 OneDrive 文件上传。对于此帐户,OneDrive 身份验证过程是不同的,即https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/aad-oauth并且它有效。
| 归档时间: |
|
| 查看次数: |
3470 次 |
| 最近记录: |