使用OneDrive API获取访问令牌 [英] Get Access Token with OneDrive API
问题描述
我正在尝试进行身份验证并登录OneDrive for business以获取访问令牌.
I am trying to authenticate and to sign to in OneDrive for business in order to get an access token.
我已经在Azure Active Directory中注册了我的应用程序,并且获得了我的client_Id和Client_Secret.基于 OneDrive API文档下一步是登录以获取将用于获取访问令牌的授权代码.我能够成功获取代码,但下一步是带有以下参数的POST:
I have registered my application in Azure Active Directory and I have got my client_Id and my Client_Secret. Base on the OneDrive API Documentation the next step is to login to get the authorization code that will be used to get the access token. I am able to get the code successfully but the next step is a POST with the following parameters:
POST https://login.microsoftonline.com/common/oauth2/token
Content-Type:应用程序/x-www-form-urlencoded
Content-Type: application/x-www-form-urlencoded
参数:
client_id:
redirect_uri:
client_secret:
code:
resource: The resource you want to access. ????
至此,我将如何知道要访问的资源,尚不清楚要为该参数发送什么值.
At this point how I am going to know the resource to access, it is not clear what value to send for this parameter.
我将其保留为空,并且出现"Access-Control-Allow-Origin"错误:
I am leaving it empty and I am getting a "Access-Control-Allow-Origin" error:
XMLHttpRequest无法加载 https://login.microsoftonline.com/common/oauth2/token一个>.所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问来源" http://localhost:23320 .响应的HTTP状态代码为400.
XMLHttpRequest cannot load https://login.microsoftonline.com/common/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:23320' is therefore not allowed access. The response had HTTP status code 400.
这是我的代码:
var bodyInfo = {
client_id: {client_id},
redirect_uri: {redirect_uri},
client_secret: {client_secret},
code: {code},
grant_type: 'authorization_code',
resource:?????
};
$.ajax({
url: "https://login.microsoftonline.com/common/oauth2/token",
type: "POST",
data: bodyInfo,
success: function (data, textStatus, jqXHR) {
window.alert("Saved successfully!");
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
我将非常感谢您的帮助.
I would really appreciate any help.
推荐答案
要了解您需要的资源,您应该使用Office的Discover api(并首先对其进行身份验证):
To know the resource you need you should firs use office's discover api (and authenticate to it first):
在大多数情况下,OneDrive for Business API端点URL是未知的.若要发现终结点URL,您需要调用Office 365发现API.要使用发现API进行身份验证,您需要请求资源 https://api.office.com/的访问令牌.发现/.确保包含结尾的/字符,否则您的应用将被拒绝访问发现API.
In most cases, the OneDrive for Business API endpoint URL will not be known. To discovery the endpoint URL, you need to make a call to the Office 365 Discovery API. To authenticate with the discovery API, you need to request an access token for resource https://api.office.com/discovery/. Make sure to include the trailing / character, otherwise your app will be denied access to the discovery API.
然后您需要获取服务数据(步骤3)
Then you need to get the service data (step 3)
GET https://api.office.com/discovery/v2.0/me/services
Authorization: Bearer {access_token}
访问令牌应位于步骤2的响应中.
The access token should be on the response for step 2.
响应应该是这样的:
{
"@odata.context": "https:\/\/api.office.com\/discovery\/v1.0\/me\/$metadata#allServices",
"value": [
{
"@odata.type": "#Microsoft.DiscoveryServices.ServiceInfo",
"capability": "MyFiles",
"serviceApiVersion": "v2.0",
"serviceEndpointUri": "https:\/\/contoso-my.sharepoint.com\/_api\/v2.0",
"serviceResourceId": "https:\/\/contoso-my.sharepoint.com\/"
}
]
}
然后,您应该找到serviceResourceId(在值数组的json对象内部),并使用它为一个驱动器获取正确的令牌(步骤4).
An then you should find the serviceResourceId (inside the json object on the value array), and use it to get the proper token for one drive (step 4).
这篇关于使用OneDrive API获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!