power bi 访问令牌 api 在邮递员中工作正常但在 angular 应用程序中不工作
power bi access token api working fine in postman but not working in angular app
我在 postman 中调用 power bi API,它运行良好,并且我得到了可接受的结果。
当我在 angular 应用程序中调用该应用程序时,它无法正常工作。
我得到的响应为空。
我在下面附上了我的代码,
请帮助我,我最近几天一直在努力解决这个问题。
API: https://login.microsoftonline.com/common/oauth2/token
下面是我的 angular 代码
getAccessToken(param): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
})
};
const body = new HttpParams()
.set('resource', 'https://analysis.windows.net/powerbi/api')
.set('client_id', 'xxxxxxxxx')
.set('client_secret', 'xxxxxxxxx')
.set('grant_type', 'password')
.set('scope', 'openid')
.set('username', 'xxxxxxxxx')
.set('password', 'xxxxxxxxx');
return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body.toString(), httpOptions)
.pipe(
map(response => response)
);}
在下面的参考图像中,相同的 API 工作正常但在 angular 中不工作。
您正在将正文作为 http 参数发布。更改为:
const body = {
resource: 'https://analysis.windows.net/powerbi/api',
client_id: 'xxxxxxxxx',
client_secret: 'xxxxxxxxx',
grant_type: 'password',
scope: 'openid',
username: 'xxxxxxxxx',
password: 'xxxxxxxxx'
}
return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body, httpOptions)
.pipe(
map(response => response)
);}
您可以这样设置页眉和正文:
const httpOptions = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-
urlencoded')
};
const params = {
resource: 'https://analysis.windows.net/powerbi/api',
client_id: 'xxxxxxxxx',
client_secret: 'xxxxxxxxx',
grant_type: 'password',
scope: 'openid',
username: 'xxxxxxxxx',
password: 'xxxxxxxxx'
}
const body = `resource=${params.resource}&client_id=${params.client_id}&client_secret=${params.client_secret}&grant_type=${params.grant_type}& scope =${params.scope}&username =${params.username}&password =${params.password}`;
return this.http.post("https://login.microsoftonline.com/common/oauth2/token",
body, httpOptions)
.pipe(
map(response => response)
);
我得到了问题和解决方案。
问题:
为什么它破坏了我的 API 回复 'null'。
问题是由于 CORS,我使用了已弃用的 crome 的 CORS 扩展。
所以给出了结果 'null'
解法:
我为 chrome 使用了最新的一个 CORS 扩展,这暂时解决了这个问题。
这不是一个合适的解决方案,因为我们无权在后端解决 cors 问题。
如果我们需要用手解决它,那么我们必须在 .net 中创建 API 以获取 AccessToken,然后 API 我们可以在 Angular.
中使用
我在 postman 中调用 power bi API,它运行良好,并且我得到了可接受的结果。 当我在 angular 应用程序中调用该应用程序时,它无法正常工作。 我得到的响应为空。
我在下面附上了我的代码, 请帮助我,我最近几天一直在努力解决这个问题。
API: https://login.microsoftonline.com/common/oauth2/token
下面是我的 angular 代码
getAccessToken(param): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
})
};
const body = new HttpParams()
.set('resource', 'https://analysis.windows.net/powerbi/api')
.set('client_id', 'xxxxxxxxx')
.set('client_secret', 'xxxxxxxxx')
.set('grant_type', 'password')
.set('scope', 'openid')
.set('username', 'xxxxxxxxx')
.set('password', 'xxxxxxxxx');
return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body.toString(), httpOptions)
.pipe(
map(response => response)
);}
在下面的参考图像中,相同的 API 工作正常但在 angular 中不工作。
您正在将正文作为 http 参数发布。更改为:
const body = {
resource: 'https://analysis.windows.net/powerbi/api',
client_id: 'xxxxxxxxx',
client_secret: 'xxxxxxxxx',
grant_type: 'password',
scope: 'openid',
username: 'xxxxxxxxx',
password: 'xxxxxxxxx'
}
return this.http.post("https://login.microsoftonline.com/common/oauth2/token", body, httpOptions)
.pipe(
map(response => response)
);}
您可以这样设置页眉和正文:
const httpOptions = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-
urlencoded')
};
const params = {
resource: 'https://analysis.windows.net/powerbi/api',
client_id: 'xxxxxxxxx',
client_secret: 'xxxxxxxxx',
grant_type: 'password',
scope: 'openid',
username: 'xxxxxxxxx',
password: 'xxxxxxxxx'
}
const body = `resource=${params.resource}&client_id=${params.client_id}&client_secret=${params.client_secret}&grant_type=${params.grant_type}& scope =${params.scope}&username =${params.username}&password =${params.password}`;
return this.http.post("https://login.microsoftonline.com/common/oauth2/token",
body, httpOptions)
.pipe(
map(response => response)
);
我得到了问题和解决方案。
问题:
为什么它破坏了我的 API 回复 'null'。
问题是由于 CORS,我使用了已弃用的 crome 的 CORS 扩展。
所以给出了结果 'null'
解法:
我为 chrome 使用了最新的一个 CORS 扩展,这暂时解决了这个问题。
这不是一个合适的解决方案,因为我们无权在后端解决 cors 问题。
如果我们需要用手解决它,那么我们必须在 .net 中创建 API 以获取 AccessToken,然后 API 我们可以在 Angular.