Power BI 访问令牌 API 在 Angular 中不工作

Power BI Access Token API not working in Angular

正在尝试将 PowerBI 仪表板嵌入到 angular 应用程序中。 API 获取访问令牌的调用在 API 中工作在 Postman 中工作但在 Angular 中不工作 $http.post

这是我的 http 请求:

Request URL:https://login.microsoftonline.com/common/oauth2/token
Request Method:POST
Status Code:400 Bad Request
Remote Address:104.211.216.34:443
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Cache-Control:no-cache, no-store
Content-Length:435
Content-Type:application/json; charset=utf-8
Date:Tue, 28 Nov 2017 12:39:04 GMT
Expires:-1
P3P:CP="DSP CUR OTPi IND OTRi ONL FIN"
Pragma:no-cache
Server:Microsoft-IIS/8.5
Set-Cookie:esctx=AQABAAAAAABHh4kmS_aKT5XrjzxRAtHzOEDuOCnOGQdXuNJYF9t_l4l8QbiGM-1SOn4WhmgW5oU1BTHew-wFbpdzDe250yG1ODXnl9crMy97-PJdBUTJD2hCZ0fjpleNFz13Xbl3nDt21xoySABfsdxlxd8ODv8ryZ_n2CwnYMpM_yEoQG5tSxlh-SdOviP8tF3-n_uBqZwgAA; domain=.login.microsoftonline.com; path=/; secure; HttpOnly
Set-Cookie:x-ms-gateway-slice=004; path=/; secure; HttpOnly
Set-Cookie:stsservicecookie=ests; path=/; secure; HttpOnly
Strict-Transport-Security:max-age=31536000; includeSubDomains
X-Content-Type-Options:nosniff
x-ms-request-id:5f039662-e0ce-425a-9e38-1593c64c7b00
X-Powered-By:ASP.NET
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-GB,en-US;q=0.9,en;q=0.8
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:188
Content-Type:application/x-www-form-urlencoded
Host:login.microsoftonline.com
Origin:http://evil.com/
Referer:http://localhost:9000/power-bi
roleId:57be85636318773723861b99
token:W0JANTc3YjEwMTY
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Form Data
view source
view URL encoded
{"grant_type":"password","client_id":"clientId","resource":"https://analysis.windows.net/powerbi/api","username":"username","password": "password"}

响应是:

correlation_id : "1e68335a-6b6d-441d-90d3-eb8c88929a37"
    error:"invalid_request"
    error_codes:[90014]
    error_description:"AADSTS90014: The request body must contain the following parameter: 'grant_type'.
    ↵Trace ID: 5f039662-e0ce-425a-9e38-1593c64c7b00
    ↵Correlation ID: 1e68335a-6b6d-441d-90d3-eb8c88929a37
    ↵Timestamp: 2017-11-28 12:39:05Z"
    timestamp:"2017-11-28 12:39:05Z"
    trace_id:"5f039662-e0ce-425a-9e38-1593c64c7b00"

谁能帮我解决这个问题??

我得到了解决方案。我得到了那个错误,我正在正确地进行 API 调用。它期望数据在 'Content-Type':'application/x-www-form-urlencoded' 中发送并且数据被序列化。

此代码有效:

$http({
      headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
      url: 'https://login.microsoftonline.com/common/oauth2/token',
      method: 'POST',
      transformRequest: $httpParamSerializer,
      transformResponse: function (x) {
        return angular.fromJson(angular.fromJson(x));
      },
      data: {
        grant_type:'password',
        client_id: "client_id",
        resource:'https://analysis.windows.net/powerbi/api',
        username:"username",
        password: "pwd",
        client_secret: "client_secret"
      }
  }) 

我希望这个答案对以后遇到同样问题的任何人都有用。