【发布时间】:2017-09-08 09:14:17
【问题描述】:
我正在尝试使用 Angular 创建一个可以通过 Web API 连接到 Dynamics CRM 的应用程序。这些是我遵循的步骤:
1. 在 Azure 中注册本机应用程序,授予所需的委派权限并更新清单以允许隐式流。
2. 在 CRM 中创建了一个应用程序用户,将其应用程序 ID 设置为我的 Azure 注册应用程序的客户端 ID。为我的应用程序用户分配了一个自定义安全角色。
3. 克隆了许多通过 ADAL 向 Azure AD 进行身份验证的 Angular 2 快速入门 Git 存储库,例如 this one。
4.更新了克隆代码的adal配置,设置了我的tenant、clientId、redirectUri和endpoints。
到目前为止,这已经成功。我能够以我的应用程序用户或作为 Azure AD 一部分的其他 CRM 用户的身份通过我的浏览器启动应用程序并登录。这将返回一个令牌。
5. 尝试向v8.0 或v8.2 发送http.get(我听说v8.2 不支持跨域调用):
getEntities(): Promise<any> {
let token = this.adalService.getCachedToken(this.adalService.config.clientId);
let headers = new Headers({
'Authentication': 'Bearer ' + token,
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'OData-MaxVersion': '4.0',
'OData-Version': '4.0'
});
let options = new RequestOptions({ headers: headers });
return this.http.get(`${crmURL}/api/data/v8.2/accounts`, options)
.toPromise()
.then((res) => { return res; })
.catch((e) => { console.error(e); });
}
6.收到此错误消息:
上面写着:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
The response had HTTP status code 401.
查看我的 Chrome 浏览器的网络选项卡,我收到两个响应:
响应 1
常规
Request URL:https://ms-dyn365-prevxxxxxx/api/data/v8.2/accounts
Request Method:OPTIONS
Status Code:200 OK
Remote Address:104.44.xxx.xxx:xxx
Referrer Policy:no-referrer-when-downgrade
标题
Access-Control-Allow-Headers:authentication,content-type,odata-maxversion,odata-version
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:http://localhost:3000
Access-Control-Expose-Headers:Preference-Applied,OData-EntityId,Location,ETag,OData-Version,Content-Encoding,Transfer-Encoding,Content-Length,Retry-After
Access-Control-Max-Age:3600
Content-Length:0
Date:Thu, 13 Apr 2017 10:08:01 GMT
Server:Microsoft-IIS/8.5
Set-Cookie:crmf5cookie=!NDyiupL55lrWWLtPQKTK52dwbxk9wdEAHeCiec0/z/7x9KWXe2dVIdQCGvL0S/HAp7F3N0OGfeWf/70=;secure; path=/
Strict-Transport-Security:max-age=31536000; includeSubDomains
Vary:Origin
X-Powered-By:ASP.NET
响应 2
常规
Request URL:https://ms-dyn365-prevxxxxx.crm4.dynamics.com/api/data/v8.2/accounts
Request Method:GET
Status Code:401 Unauthorized
Remote Address:104.xx.xxx.xxx:xxx
Referrer Policy:no-referrer-when-downgrade
标题
Cache-Control:private
Content-Length:49
Content-Type:text/html
Date:Thu, 13 Apr 2017 10:08:01 GMT
REQ_ID:b2be65bc-xxxx-4b34-xxxx-5c39812650xx
Server:Microsoft-IIS/8.5
Set-Cookie:ReqClientId=xxxxxxxx-70b5-45f9-9b84-30f59481bxxx; expires=Wed, 13-Apr-2067 10:08:01 GMT; path=/; secure; HttpOnly
Strict-Transport-Security:max-age=31536000; includeSubDomains
WWW-Authenticate:Bearer authorization_uri=https://login.windows.net/xxxxxxxx-87e4-4d81-8010-xxxxxxxxxxxxx/oauth2/authorize, resource_id=https://ms-dyn365-prevxxxxxx.crm4.dynamics.com/
X-Powered-By:ASP.NET
注意:我可以通过 Postman 成功访问 Web API:
1.我在 Azure 中为我的应用程序输入 https://www.getpostman.com/oauth2/callback 作为回调 URL。
2.我打开Postman,设置参数如下,按Request Token:
Token Name: Token
Auth URL: https://login.windows.net/common/oauth2/authorize?resource=https://ms-dyn365-prevxxxxxx.crm4.dynamics.com
Access Token URL: https://login.microsoftonline.com/common/oauth2/token
Client ID: xxxxxxxx-ebd3-429c-9a95-xxxxxxxxxxxx
Callback URL: https://www.getpostman.com/oauth2/callback
Grant Type: Authorization Code
3.这会打开一个我登录的网页。
4.返回一个令牌,我将其添加到 Postman GET 标头中:
Content-Type: application/json
Authorization: Bearer eyJ0eXAiO...
5. 在 Postman 中发送 GET:
GET https://ms-dyn365-prevxxxxxx.crm4.dynamics.com/api/data/v8.2/accounts
6.帐户已成功返回。
如果我在我的应用程序中使用相同的令牌,我仍然会收到 401 错误。
【问题讨论】:
-
在 Azure 中注册应用时,是否将“localhost:3000”设置为“登录 URL”?
-
@PawelGradecki 我实际上在 Azure 中看不到“登录 URL”选项(我使用的是较新的 Azure 门户)。我可以看到“主页”和“重定向 URI”都设置为“localhost:3000”。
-
还有其他查询有效吗?例如,您是否尝试过获取帐户?因为目前您正在查询元数据,所以我的猜测是即使使用 CORS 也可能无法实现(尽管我没有找到任何信息来支持这个假设)
-
你也应该在你的获取请求中使用“application/json”而不是“application/jsonp”
-
@PawelGradecki 我试过 /api/data/v8.2/accounts 并收到同样的错误。
application/jsonp是我的问题中的一个错误,我的代码实际上使用了json,对不起!我现在已经编辑了我的问题。
标签: angular azure dynamics-crm azure-active-directory adal