OneDrive API: 未经身份验证,必须经过身份验证才能使用“/drive”语法
OneDrive API: Unauthenticated, Must be authenticated to use '/drive' syntax
我正在使用 OneDrive api 在 Rails 应用程序上的 Ruby 中上传文件,OneDrive API 开始在使用端点上传文件时出现未经身份验证的错误 /drive/root:/#{文件名}:/内容。错误如下:
{"error"=>{"code"=>"unauthenticated", "message"=>"Must be authenticated to use '/drive' syntax"}}
然后我通过使用范围 files.readwrite offline_access.
遵循 OneDrive 文档获得了一个新的 refresh_token
对于 OneDrive 身份验证,我正在向端点 https://login.microsoftonline.com/common/oauth2/v2.0/token 发送 POST 请求,以使用 refresh_token 和以下 headers 和 body:
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
任何人都可以帮助我为什么我会收到未经身份验证的错误或者我如何使用“/drive”语法进行身份验证?
提前致谢!
已解决:
在我的例子中,我使用 "Code flow" 进行身份验证,并使用以下 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
访问上面的 url 打开重定向 url,其中包含一个长 code
参数,我用它来获取 access_token
和 refresh_token
但是 access_token 无法将文件上传到 OneDrive 并重新调整 "unauthenticated" 问题中提到的错误。
经过研究,我发现我用来获取 code
进行 OneDrive 身份验证的 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
在浏览器中访问上面的 url 将我重定向到带有代码参数的页面,但它是像 K9vb4e786-afg6-1a3b-1234-12abc01234ca
.
这样的小代码
我使用此代码通过以下 POST 请求获得 access_token
和 refresh_token
:
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 which was incorrect. Then I followed Microsoft Account authentication ie, 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 并且有效。
我正在使用 OneDrive api 在 Rails 应用程序上的 Ruby 中上传文件,OneDrive API 开始在使用端点上传文件时出现未经身份验证的错误 /drive/root:/#{文件名}:/内容。错误如下:
{"error"=>{"code"=>"unauthenticated", "message"=>"Must be authenticated to use '/drive' syntax"}}
然后我通过使用范围 files.readwrite offline_access.
遵循 OneDrive 文档获得了一个新的 refresh_token对于 OneDrive 身份验证,我正在向端点 https://login.microsoftonline.com/common/oauth2/v2.0/token 发送 POST 请求,以使用 refresh_token 和以下 headers 和 body:
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
任何人都可以帮助我为什么我会收到未经身份验证的错误或者我如何使用“/drive”语法进行身份验证?
提前致谢!
已解决:
在我的例子中,我使用 "Code flow" 进行身份验证,并使用以下 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
访问上面的 url 打开重定向 url,其中包含一个长 code
参数,我用它来获取 access_token
和 refresh_token
但是 access_token 无法将文件上传到 OneDrive 并重新调整 "unauthenticated" 问题中提到的错误。
经过研究,我发现我用来获取 code
进行 OneDrive 身份验证的 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
在浏览器中访问上面的 url 将我重定向到带有代码参数的页面,但它是像 K9vb4e786-afg6-1a3b-1234-12abc01234ca
.
我使用此代码通过以下 POST 请求获得 access_token
和 refresh_token
:
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 which was incorrect. Then I followed Microsoft Account authentication ie, 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 并且有效。