rest - OneDrive API : Unauthenticated, 必须经过身份验证才能使用 '/drive' 语法

标签 rest api authentication offline onedrive

我正在使用 OneDrive api 在我的 Ruby on Rails 应用程序中上传文件,而 OneDrive API 开始在使用端点/drive/root:/#{filename}:/content 上传文件时出现未经身份验证的错误。错误如下:

{"error"=>{"code"=>"unauthenticated", "message"=>"Must be authenticated to use '/drive' syntax"}}

然后我通过使用范围 files.readwrite offline_access 遵循 OneDrive Docs 获得了一个新的 refresh_token。

对于 OneDrive 身份验证,我将 POST 请求发送到端点 https://login.microsoftonline.com/common/oauth2/v2.0/token使用带有以下 header 和正文的 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>",
}

我是否使用正确的端点从 refresh_token 获取 access_token?

我用来将文件上传到 OneDrive 的基本 uri 是 https://api.onedrive.com/v1.0

任何人都可以帮助我为什么我收到未经身份验证的错误或者我如何使用“/驱动器”语法进行身份验证?

提前致谢!

最佳答案

已解决:

就我而言,我使用“代码流”进行身份验证并使用以下网址获取 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

访问上面的url打开重定向url很长code我用来获取 access_token 的参数和 refresh_token但该 access_token 无法将文件上传到 OneDrive 并重新调整所提到的“未经身份验证”错误。

经过研究,我发现我用来获取code的url OneDrive 身份验证适用于 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

在浏览器中访问上述 url 也将我重定向到带有代码参数的页面,但它是像 K9vb4e786-afg6-1a3b-1234-12abc01234ca 这样的小代码。 .

我用这个代码得到 access_tokenrefresh_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)

此请求返回 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它奏效了。

关于rest - OneDrive API : Unauthenticated, 必须经过身份验证才能使用 '/drive' 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50036233/

相关文章:

c# - 如何在请求中传递字符串列表?

javascript - Facebook 未授予访问用户上传照片的权限

twitter-bootstrap - 禁用 Bootstrap 数据属性 API?

php - 如何将API蓝图与API源代码同步?

java - 如何使用 Jersey 2 测试框架为此类编写单元测试

使用 JWT 联合身份的 REST 身份验证/授权

javascript - 如何将数据发布到 JsonPlaceholder 假服务器?

具有远程 jwt 身份验证的 Django REST

android - 为什么我从 google appengine 登录而不是 ACSID 获取 SACSID cookie?

php - 谷歌登录集成